How do I deeply eager load an entity with a reference to an instance of a persistent base type (Entity Framework 4)
Above is a simplified version of our domain model. NotificationOrder
has a reference to an instance of a sub class (consider ReferenceNumberBase
logically abstract).
Problem:
I want a query to return all NotificationOrders
that satisfies X开发者_JAVA百科YZ and I want that query to eagerly load all referenced instances of CustomerCase
(including all related objects of that graph, except Group
forget about that issue for the moment).
I've tried searching for a solution to this, but all I've found are solutions to problems equivalent of querying for CustomerCase
as a root object directly.
I'd like something like this:
var query = ObjectContext.CreateObjectSet<NotificationOrder>.Where(e => e.NotificationType == "Foo");
return ((ObjectSet<NotificationOrder>) query).Include("ReferenceNumberBase");
However, that won't load the Vehicle
instance of CustomerCase
or any of the other related objects. How can I express this so EF understands the eager load I want (I'd very much like to avoid multiple roundtrips / notification order)?
NOTE: Since CustomerCase
is a derived type I can't do normal transitive include using something like this:
var query = ObjectContext.CreateObjectSet<NotificationOrder>.Where(e => e.NotificationType == "Foo");
return ((ObjectSet<NotificationOrder>) query).Include("ReferenceNumberBase.Vehicle"); //
since the Vehicle
property is a member of the derived CustomerCase
type, and not the ReferenceNumberBas
e type and instead we get errors like:
"The EntityType 'Model.ReferenceNumberBase' does not declare a navigation property with the name 'Vehicle'."
Neither can I use query.OfType<CustomerCase>...
since the query type is NotificationOrder
, and not ReferenceNumberBase (or can I somehow?).
ps. We are using self tracking POCO entities with EF4 (have not upgraded to 4.1 yet)
EDIT: I've searched some more, and as of about a year ago this looks to have been a limitation of the Include() method (at least at that time). Is this accurate and has this been adressed since then? [sources below]
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/a30351ab-5024-49a5-9eb4-798043a2b75d
http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1057763-inheritance-eager-loading?ref=title
https://connect.microsoft.com/VisualStudio/feedback/details/594289/in-entity-framework-there-should-be-a-way-to-eager-load-include-navigation-properties-of-a-derived-class
精彩评论