I have an object hierarchy that looks something like this:
public class Book
{
public virtual List<Page> Pages { get; set; }
public virtual List<Paragraph> Paragraphs { get; set; }
}
public class Page
{
public virtual List<Paragraph> Paragraphs { get; set; }
}
I want to load the complete object hierarchy and am going about that like this:
Book book = (from b in context.Books.Include("Pages").Include("Paragraphs")
.Include("Pages.Paragraphs") where CONDITION).SingleOrDefault();
I find that book.Pages and book.Paragraphs are loaded, but book.Pages[i].Paragraphs is null.
Examining the database, the data looks correct (association columns are all correctly populated).
I also tried the lamda syntax, but do not see how that could work when the parameter is a collection rather than an entity, e.g. one can do something like this:
.Include(s => s.Paragraphs.Select(p => p.Id == 1)
but I do not see how one could use the lamda syntax to specify that the Paragraphs collection for each Page in book.Pages should be loaded.
Am I missing something, or is this a limitation of Entity Framework? If it’s a limitation, how can I work around it?
In the actual code (not the code boiled down for the purpose of asking a targeted question, I was missing a
virtualkeyword. EF didn’t complain about an inability to load the additional data requested through.Include("Pages.Paragraphs"), it just silently ignored that request.