EF Core Collection Load .. of a Collection

纵然是瞬间 提交于 2021-02-06 10:46:29

问题


Using EF Core 1.1.0

I have a model that has collections that themselves have collections.

public class A {  
  public string Ay {get;set;}    
  public List<B> Bees {get;set;}
}

public class B {
  public string Be {get;set;}
  public List<C> Seas {get;set;}
}

public class C {
  public string See {get;set;}
  public bool InDark {get;set;}
  public List<D> Sigh {get;set;}
}

Now.. A is huge, and 99% of the time I don't care about B, so it doesn't get loaded. If I did load it, then it would be something like:

context.A.Include(a=>a.Bees).ThenInclude(b=>b.Seas).ThenInclude(c=>c.Sigh)...

Now let's say I already have A loaded up and the 1% happens for me to care about B. We don't have lazy loading yet, but the last release did give us explicit loading. Awesome.

context.Entry(A).Collection(a=>a.Bees).Load();

Problem seems to be that there isn't a way to include the additional collections inside B? Do I have no choice but to reload A with the .Include.ThenInclude.ThenInclude.Etc?


回答1:


Fortunately you have an option. Instead of directly calling Load, you can use Query method and apply as many Include / ThenInclude as you wish.

For your sample, it would be something like this:

context.Entry(A).Collection(a => a.Bees)
    .Query()
    .Include(b => b.Seas).ThenInclude(c => c.Sigh)...
    .Load();


来源:https://stackoverflow.com/questions/41529100/ef-core-collection-load-of-a-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!