How do I do multiple level loading in Entity Framework 4.3 when I have a polymorphic collection?

I have the following classes:

public class Parent 
  public ICollection<Child> Children {get;set;}

public class Child

public class Boy : Child
  public Toy Toy {get;set;}    

public class Girl : Child
  public Book Book {get;set;}


I want to happily load a parent with all the boys:

Parents.Include(p => p.Children.OfType<Boys>().Select(b => b.Toy));


The above does not work and I am getting an error stating that the path is not valid.

How to do it?


source to share

1 answer

I think in this case the extension method Include

resolves the string "Boys" which obviously cannot be included by the member method Include


Even if it does, I suspect it would be a challenge to have collections Children

that are only populated with objects Boy

. For me this would be the undefined state of the collection as it represents the children of the parent. Thus, it must contain all children or still be empty.

If you often need a Boys collection (or real life pendant) and its links ( Toy

), you should map it as a separate navigation property. Otherwise, do OfType()

in the Children collection.



All Articles