I can´t find a way to load an object from a DB with all its related objects. Concerning this simplified model (ID-Properties not shown):
class MainClass
{
public virtual ICollection<FirstLevelClass> FirstLevelObjects {get; set;}
}
class FirstLevelClass
{
public virtual ICollection<SecondLevelClassA> SecondLevelObjectsA {get; set;}
public virtual ICollection<SecondLevelClassB> SecondLevelObjectsB {get; set;}
}
class SecondLevelClassA
{
public virtual int SomeValue {get; set;}
}
class SecondLevelClassB
{
public virtual int SomeValue {get; set;}
}
The DbContext is on "MainClass"-Objects:
public SampleContext : DbContext
{
public DbSet<MainClass> MainClassObjects {get; set;}
}
How can I load an MainClass-object from db with all first- and second-level-objects? I can do:
using (var context = new SampleContext())
{
var MainClassObjects = context.MainClassObjects.Include(p => p.FirstLevelObjects).ToList();
// ...move objects from the context to program logic...
}
but how do I get SecondLevelObjects? I´m missing something like:
using (var context = new SampleContext())
{
var MainClassObjects = context.MainClassObjects.Include(p => p.FirstLevelObjects.SecondLevelObjects).ToList();
// ...move objects from the context to program logic...
}
Is this even possible or do I have to adapt the DbSets in the DbContext?