entity-framework – EF代码优先:如何加载相关数据(父子孙子)?

我有这个实体:

public class DynamicPage {

    public int PageId { get; set; }

    public int Order { get; set; }

    public string MenuText { get; set; }

    public string MenuHover { get; set; }

    public int? ParentId { get; set; }

    public virtual DynamicPage Parent { get; set; }

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

该实体可能有3个级别:父级 – >孩子 – >孙.如何加载父级(级别1)以及所有关联子级(级别2)和每个子级,关联孙级(级别3)(如果有)?感谢帮助.

EF 4.1功能和语法:

var entity = context.Parents
    .Include(p => p.Children.Select(c => c.GrandChildren))
    .FirstOrDefault(p => p.Id == 1); // or whatever condition
https://stackoverflow.com/questions/7508241/ef-code-first-how-to-load-related-data-parent-child-grandchild

转载注明原文:entity-framework – EF代码优先:如何加载相关数据(父子孙子)?