【问题标题】:Explicit loading do not loading related entities(list in list)显式加载不加载相关实体(列表中的列表)
【发布时间】: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


【解决方案1】:

您实际上是向数据库发出 3 个请求以获得所需的结果。实现这一目标的更好、优化的方法可能是

[HttpPost("EnterUserDetail")]
public async Task<ActionResult<RegistrationUser>> postUserDetail( RegistrationUser registrationUser)
{
    // send one query to database to get the result and include here.
    var ldetails = _context.RegistrationUsers.Include(i => i.ListFriends).SingleOrDefault(c => c.UserName == registrationUser.UserName && c.Password == registrationUser.Password);

    if (ldetails == null && pdetails == null)
    {
        return NotFound();
    }

    return ldetails;
}

【讨论】:

  • 是的,谢谢。正确的是: var ldetails = _context.RegistrationUsers.Include(i => i.ListFriends).ThenInclude(z=>z.MessagesDetails).SingleOrDefault(c => c.UserName == registrationUser.UserName && c.Password ==注册用户.密码);
最近更新 更多