2

I have the following classes:

public class Box
{
    public int Id { get; set; }
    public DbSet<Item> Items { get; set; }
}

public abstract class Item
{
    public int Id { get; set; }
}

public class Toy : Item
{
    public string Name { get; set; }
}

public class ElectricToy
    : Toy
{
    public int Voltage { get; set; }
}

public class WoodenToy
    : Toy
{
    public string Color { get; set; }
}

A Box has a collection of items which might be electric toys or wooden toys. Now, I want to load all toys in a box with the following code.

var boxes = 
    db.Boxes
        .Where(o => o.Id = id)
        .Include(o => o.Items);

It loads all toy but it doesn't load electric and wooden toy values, that is Voltage is always 0 and Color is always null.

How to force Entity Framework to load values for ElectricToy and WoodenToy?

Anonymous
  • 9,366
  • 22
  • 83
  • 133
  • @SAm yes, it is. I just updated code sample. :) – Anonymous Aug 31 '14 at 10:56
  • Possible duplicate of http://stackoverflow.com/questions/6263288/eager-loading-property-of-derived-class-using-include – dotNET Aug 31 '14 at 11:00
  • @dotNET No, it isn't. That question wants to download a specific type from DbSet while I want to download all properties of subtype of a property of DbSet. Entity's structure is difference. – Anonymous Aug 31 '14 at 11:02
  • If it's one level inheritance then everything is fine. I only face this problem when it has multiple-level of inheritance. – Anonymous Aug 31 '14 at 11:06

0 Answers0