Unwanted loading, including navigation property of a derived class

Class structure example

class Order { public int Id { get; set; } public DateTime Date { get; set; } public List<OrderDetail> Details { get; set; } } class OrderDetail { public int Id { get; set; } public int Qty { get; set; } public Item Item { get; set; } } class Item { public int Id { get; set; } public string Name { get; set; } } class ElectronicItem : Item { public MoreDetail Detail { get; set; } } class MoreDetail { public int Id { get; set; } public string SomeData { get; set; } } 

To fill the order object with all the navigation properties, I wrote

 context.Orders.Include("Details").Include("Details.Item") 

I also want to load the MoreDetail object, so I tried

 context.Orders.Include("Details").Include("Details.Item.Detail") 

This did not work. How to download a complete order item?

+4
source share
1 answer

This is currently not possible, but it is a feature requested by the User DataVoice community, as you already found. There is also a related error in MS Connect .

You simply cannot aim to load the navigation properties of derived types, but you can load them with a separate request:

 var moreDetails = context.MoreDetails; 

EF should automatically fix your navigation properties. If you use order filtering in the original query, you should also apply this filter to a more detailed query:

 var moreDetails = cotnext.MoreDetials.Where(m => m.Item.Order ....); 
+1
source

All Articles