EF code-first: How to load related data (parent-child-grandchild)?

后端 未结 2 1807
猫巷女王i
猫巷女王i 2020-12-18 02:10

I have this entity:

public class DynamicPage {

    public int PageId { get; set; }

    public int Order { get; set; }

    public string MenuText { get; se         


        
相关标签:
2条回答
  • 2020-12-18 02:51

    If you want to make life easy on yourself, follow the EF Code First conventions of naming your table IDs simply Id (or, alternatively, name of table + Id, e.g., DyanmicPageId).

    This should leave you with something like this:

    public class DynamicPage
    {
        public int Id { 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; }
    }
    

    Then you need to set up the relationship between parents and children explicitly in an OnModelCreating method in your DbContext class.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<DynamicPage>()
            .HasMany(page => page.Children)
            .WithRequired(child => child.Parent)
            .HasForeignKey(child => child.ParentId);
    }
    

    You can then select children or grandchildren as needed:

    var parent = dbContext.DynamicPages.Where(page => page.ParentId == null);
    var children = parent.Children;
    var grandchildren = parent.SelectMany(page => page.Children);
    var allRelatedPages = parent.Union(children).Union(grandchildren);
    
    0 讨论(0)
  • 2020-12-18 03:08

    EF 4.1 feature and syntax:

    var entity = context.Parents
        .Include(p => p.Children.Select(c => c.GrandChildren))
        .FirstOrDefault(p => p.Id == 1); // or whatever condition
    
    0 讨论(0)
提交回复
热议问题