EntityFramework Include (Eager Load) virtual property of virtual property

Imagine we have three Dbsets as shown below:

Category
{
...
   public virtual ICollection<Item> Items {get; set;}
...
}

Item
{
...
   public virtual ICollection<Specification> Specifications{get; set;}
...
}

Specification
{
...
}

      

For active 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 also load the collection's subplots?

PS: I tried to remove the virtual

test keyword (I don't want to remove it) but it doesn't work either.

+3


source to share


2 answers


You can also use the notation

db.Categories.Include("Items.Specifications")

      



Please note that this must be the line

+5


source


 Category cat = db.Categories
        .Include(c => c.Items.Select(s => s.Specifications))
        .FirstOrDefault(c => c.Id == 1);

      



+6


source







All Articles