Imagine that we have three Dbsets like below:
Category
{
...
public virtual ICollection<Item> Items {get; set;}
...
}
Item
{
...
public virtual ICollection<Specification> Specifications{get; set;}
...
}
Specification
{
...
}
For eager loading I use it like this:
Category cat = db.Categories.Include(c=> c.Items).FirstOrDefault(c=> c.Id == 1);
but now the problem is that
cat.Items[0].Specifications
is null
, How can we make it to eager load sub collections of the collection too?
P.S.: I tried removing the virtual
keyword for testing (I'm don't want to remove it) but it didn't work either.
Now we can also use:
db.Categories.Include(c => c.Items)
.ThenInclude(i => i.Specifications);
You can also use the notation
db.Categories.Include("Items.Specifications")
Note it has to be a string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With