集合的EF核心收集负载.. [英] EF Core Collection Load .. of a Collection
问题描述
使用EF Core 1.1.0
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;}
}
现在.. A很大,而且99%的时间我不在乎B,因此它不会被加载.如果我确实加载了它,那么它将是这样的:
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)...
现在假设我已经加载了A ,而1%的发生让我关心B.我们还没有延迟加载,但是最新版本确实为我们提供了显式加载.很棒.
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();
问题似乎是没有办法在B内包含其他集合吗?除了将.Include.ThenInclude.ThenInclude.Etc重新加载A之外,我别无选择吗?
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?
推荐答案
幸运的是,您可以选择.可以直接使用 Query
方法,并根据需要应用尽可能多的 Include
/ ThenInclude
,而不是直接调用 Load
.
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();
这篇关于集合的EF核心收集负载..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!