我首先从数据库中加载了所有消息,然后选择了所需的内容。
但这不是全部加载。
var ldetails = _context.RegistrationUsers.Include(i => i.ListFriends).ThenInclude(z => z.MessagesDetails).SingleOrDefault(c => c.UserName == Context.User.Identity.Name); < / p>
我只想按标准加载一条消息,但我经常遇到错误。 请告诉我一种解决方法。
我尝试过
var ldetails = _context.RegistrationUsers.Include(i => i.ListFriends).ThenInclude(z=>z.MessagesDetails.SingleOrDefault(g=>g.TextMessage == messagesDetail.TextMessage)).SingleOrDefault(c => c.UserName == Context.User.Identity.Name);
但是我得到一个错误(
public class RegistrationUser
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PMId { get; set; }
[Required]
[Column(TypeName ="varchar(16)")]
public string UserName { get; set; }
[Required]
[Column(TypeName = "varchar(16)")]
public string Password { get; set; }
[Column(TypeName = "varchar(480)")]
public string Token { get; set; }
public ICollection<ListFriend> ListFriends { get; set; }
public RegistrationUser()
{
ListFriends = new List<ListFriend>();
}
}
public class ListFriend
{
[Key,Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Required]
[Column(TypeName ="varchar(16)")]
public string UserFriendName { get; set; }
public ICollection<MessagesDetail> MessagesDetails { get; set;
}
public ListFriend()
{
MessagesDetails = new List<MessagesDetail>();
}
}
public class MessagesDetail
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key,Column(Order = 0)]
public int PMIdMes { get; set; }
[Required]
[Column(TypeName ="nvarchar(240)")]
public string TextMessage{ get; set; }
[Required]
[Column(TypeName = "varchar(16)")]
public string SenderUser { get; set; }
[Required]
[Column(TypeName = "varchar(16)")]
public string CatcherUser { get; set; }
}
//My method
[Authorize]
public async Task DeleteMessage(string frend ,MessagesDetail
messagesDetail)
{
var ldetails = _context.RegistrationUsers.Include(i =>
i.ListFriends).ThenInclude(z=>z.MessagesDetails.
SingleOrDefault(g=>g.TextMessage == messagesDetail.TextMessage)).
SingleOrDefault(c => c.UserName == Context.User.Identity.Name);
//.....
}
I expect to receive a message for the condition:
g=>g.TextMessage == messagesDetail.TextMessage;
答案 0 :(得分:0)
答案 1 :(得分:-1)
var pdetails = _context.RegistrationUsers.SingleOrDefault(c=>c.Password==Context.User.Identity.Name);
var nlk = _context.Entry(pdetails).Collection(n => n.ListFriends).Query().SelectMany(n => n.MessagesDetails)
.Where(z => z.TextMessage == messagesDetail.TextMessage).ToList();
工作!