简体   繁体   中英

Parent null in Entity Framework

I have a Group class that allows nested Groups. 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.

Why is this happening?

Group.Parent isn't virtual. Hence, lazy loading doesn't triggers here.
Either make it virtual, or use Include(g => g.Parent) , when loading child directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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