1

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?

2 Answers 2

2

One solution can be to use Include method's other overload which takes a string like this:

var MainClassObjects = context.MainClassObjects
.Include("FirstLevelObjects")
.Include("FirstLevelObjects.SecondLevelObjects")
.ToList();

Update:

When you're using this method you just need to determine the paths you want to fetch so for updated question you can use this:

var MainClassObjects = context.MainClassObjects
.Include("FirstLevelObjects.SecondLevelObjectsA")
.Include("FirstLevelObjects.SecondLevelObjectsB")
.ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

In fact, if you use line Include("FirstLevelObjects.SecondLevelObjects") you don't even need to use Include("FirstLevelObjects")
I even tried that before, but it didn´t work at all. I couldn´t even include FirstLevelObjects only. Maybe I was missing something, I will try again.
It works now, I have no idea what I was missing before. Thanks! But also using this way, I´m nit quite sure how to include two List-Properties of one object. (See updated question).
Your edited answer (with two different SecondLevelObjects) works perfectly, thanks again!
1

Try this:

using (var context = new SampleContext())
{
    var MainClassObjects = context.MainClassObjects.Include(p => p.FirstLevelObjects.SelectMany(s => s.SecondLevelObjects)).ToList();

// ...move objects from the context to program logic...

}

You should add a .Select() statement to get an object or a list.

2 Comments

this works so far, thanks! .SelectMany() gives an ArgumentException although SecondLevelObjects is a list. .Select does the job. but now I´m facing another problem: how do I include two "List-Properties" in one class? (Question above updated)
My bad, it also should be .Select() for a list. Answer updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.