简体   繁体   English

实体框架中的父级null

[英]Parent null in Entity Framework

I have a Group class that allows nested Groups. 我有一个允许嵌套组的Group类。 My setup is like so: 我的设置是这样的:

class Group
{
    [InverseProperty("Children")]
    public Group Parent { get; set; }

    public virtual ICollection<Group> Children { get; set; }
}

If I retrieve a folder with children, they are populated and their Parent field is filled in. If I just get a child folder directly Parent is null. 如果我检索到带有子文件夹,则将填充它们,并填写其“父级”字段。如果我直接得到一个子文件夹,则“ Parent为null。

Why is this happening? 为什么会这样呢?

Group.Parent isn't virtual. Group.Parent不是虚拟的。 Hence, lazy loading doesn't triggers here. 因此,延迟加载不会在这里触发。
Either make it virtual, or use Include(g => g.Parent) , when loading child directly. 直接加载子级时,要么使其虚拟,要么使用Include(g => g.Parent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM