EF 4.1,Code-First:急切加载级联集合

Sla*_*uma 15 .net entity-framework ef-code-first entity-framework-4.1

如果我有以下班级模型......

public class A
{
    public int AId { get; set; }
    public ICollection<B> BCollection { get; set; }
}

public class B
{
    public int BId { get; set; }
    public ICollection<C> CCollection { get; set; }
}

public class C
{
    public int CId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

...是否可以A从包含所有级联集合的数据库中急切加载类型的对象?

我可以这样包括BCollection:

A a = context.ASet.Where(x => x.AId == 1)
          .Include(x => x.BCollection)
          .FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

我是否还可以以某种方式CCollection包含所有已加载的B对象,以便A使用单个数据库查询来获取内存中的所有依赖对象?

Lad*_*nka 22

使用.Include(x => x.BCollection.Select(b => b.CCollection))也在这里描述.

它也适用于级联.每次你需要加载导航属性,这是集合使用.Select.