【发布时间】:2025-11-29 18:30:02
【问题描述】:
我尝试从已找到的用户加载朋友列表,并从朋友列表中加载每个消息列表。
不工作 只加载好友,不要在每个好友中加载消息
_context.Entry(ldetails).Collection(p=>p.ListFriends).Query()。 包括(r=>r.MessagesDetails).Load();
我的数据结构
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; }
}
我的方法不行
[HttpPost("EnterUserDetail")]
public async Task<ActionResult<RegistrationUser>> postUserDetail( RegistrationUser registrationUser )
{
var ldetails = _context.RegistrationUsers.SingleOrDefault(c=>c.UserName==registrationUser.UserName);
var pdetails = _context.RegistrationUsers.SingleOrDefault(c=>c.Password==registrationUser.Password);
if (ldetails == null && pdetails == null)
{
return NotFound();
}
//_context.Entry(ldetails).Collection("ListFriends").Load();
_context.Entry(ldetails).Collection(p=>p.ListFriends).Query().Include(r=>r.MessagesDetails).Load();
//_context.Entry(ldetails).Collection("ListFriends").IsLoaded = true;
//await _context.SaveChangesAsync();
return ldetails;
}
【问题讨论】:
-
不清楚什么“不起作用”。
标签: c# .net entity-framework .net-core