2

Using EF Code First and given an Entity that contains a List, how can I eagerly load the entire object graph for that entity:

Example:

public class Foo
{
    public int Id { get; set; }

    public List<BarBase> Bars { get; set; }
}    

public class BarBase
{
    public int Id { get; set; }

    public string Text { get; set; }
}

public class BarTypeA : BarBase
{
    public List<Baz> Bazes { get; set; }
}    

public class BarTypeB : BarBase
{
    public List<Quux> Quuces { get; set; } { get; set; }
}   

If BarBase were not a base class that could contain instances of several different subtypes, I could use

.Include("Bars").Include("Bars.Bazes")

If I try

.Include("BarBase").Include("BarBase.Bazes").Include("BarBase.Quuces") 

I get the error

A specified Include path is not valid. The EntityType 'BarBase' does not declare a navigation property with the name 'Bazes'.

But how do I handle the situation that Bars can contain different concrete types, and I want to eagerly load all of those instances including the List<T> contained in those concrete types?

Eric J.
  • 147,927
  • 63
  • 340
  • 553

1 Answers1

0

This is reported problem in EF currently without a solution.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • This similar question has an accepted solution, but I don't understand the solution. Do you agree that the accepted answer will solve this category of problems? http://stackoverflow.com/questions/7635152/entity-framework-eager-loading-of-subclass-related-objects – Eric J. May 23 '12 at 20:22
  • Yes it should work. It is nice workaround but it probably produces complex SQL query. – Ladislav Mrnka May 24 '12 at 20:01