If I have the following class model …
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; }
}
… is it possible to eager-load an object of type A from the database with all cascading collections included?
I can include the BCollection like so:
A a = context.ASet.Where(x => x.AId == 1)
.Include(x => x.BCollection)
.FirstOrDefault();
Can I also include somehow the CCollection of all loaded B objects so that I get A with all dependent objects in memory with a single database query?
Use
.Include(x => x.BCollection.Select(b => b.CCollection))also described here.It works also for cascade. Every time you need to eager load navigation property which is collection use
.Select.