Entity Framework Feature Suggestions

Eager loading for properties of derived classes

In entity framework there should be a way to eager load (include) navigation properties of a derived class.

When in an data model for entity framework has a navigation property it is not posseble to eager load that navigation property besides when using OfType<> or when eager loading the derived type itself by a navigation property.
This could be done by using a special syntax of the include path.
Since a property of the base class can not have the same name as the derived class, it would also be possible to navigate to the derived class by its name.

e.g.: If class Derived inherits from Base and has a navigation property named Prop one could say
dbContext.BaseSet.Include("Derived.Prop")

this should query all entities of type Base and eager load property Prop for all instances of Derived in the reslult list

434 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Christof NordiekChristof Nordiek shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    Offir ShvartzOffir Shvartz shared a merged idea: Inheritance eager loading  ·   · 
    Diego VegaAdminDiego Vega (Development Lead, DataFx) responded  · 

    Reverting status from “under review” to “no status” since we aren’t currently working on this. We will still consider it for future releases.

    20 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • EricEric commented  ·   ·  Flag as inappropriate

        Hmmm 386 votes and this moves from "under review" to "no status". I certainly hope this becomes a priority at some point. The current solution is terribly inefficient.

      • Ran DavidovitzRan Davidovitz commented  ·   ·  Flag as inappropriate

        This is a must in real life scenarios (not the simple demos). today i was forced to remove inheritance!!

      • HiteshHitesh commented  ·   ·  Flag as inappropriate

        It should also support navigation properties of navigation property
        For example, Orders -> OrderDetails -> Items

        ElectronicItem derives from Items
        ElectrionicItem -> MoreDetails

        Hence, it will be

        Orders -> OrderDetails -> ElectronicItem (Items) -> MoreDetails

        Now, I want to retrieve Order including all navigation properties

        e.g.
        context.Orders.Include("OrderDetails").Include("OrderDetails.Items").Include("OrderDetails.Items.MoreDetails");

      • M. HolmgrenM. Holmgren commented  ·   ·  Flag as inappropriate

        No, eager loads should be explicit.

        However, there should be support for including sub type property navigation includes which today forces multiple queries

      • M. HolmgrenM. Holmgren commented  ·   ·  Flag as inappropriate

        Context.CreateObjectSet<BaseType>.Where(o => o ...).Include("BasetypeNavigationProperty").Include<Subtype1>("SubTypeNavigationProperty").Include<Subtype2>("OtherSubtype2Property")

        This is what I want in an eager load query

      • Francis OuelletFrancis Ouellet commented  ·   ·  Flag as inappropriate

        If I have this :
        Invoice 1 -- * Amounts
        Amounts -> UnitTax or Fee

        I want to load invoice and his dependances

        IQueryable<Invoice> query = from f in context.Set<Invoice>()
        .Include("Amounts")
        .Include(f => (f.Amounts as UnitTax).UnitTaxDetail)
        .Include(f => (f.Amounts as Fee).FeeDetail)
        where f.ID.Equals(id)
        select f;

        Not working!

      • Bruno de Souza MeloBruno de Souza Melo commented  ·   ·  Flag as inappropriate

        Solution.

        The derived type it's applied only for Conceptual Data Model (Entity Data Model). Remove any foreignkey of your derived physical table.

        Ex.:

        Car - IDCar, Name

        SportCar - IDCar, WheelsType

        Remove the foreignkey (don't remove the column just relationship for using on Mapping Detail) of SportCar and set BaseType to Car in Entity Data Model.

        See the article association with derived types.

        http://msdn.microsoft.com/en-us/library/dd163157.aspx

        Thank you.

      • Bruno de Souza MeloBruno de Souza Melo commented  ·   ·  Flag as inappropriate

        Solution.

        The derived type it's applied only for Conceptual Data Model (Entity Data Model). Remove any foreignkey of your derived physical table.

        Ex.:

        Car - IDCar, Name

        SportCar - IDCar, WheelsType

        Remove the foreignkey (don't remove the column just relationship for using on Mapping Detail) of SportCar and set BaseType to Car only in Entity Data Model.

        See the article association with derived types.

        http://msdn.microsoft.com/en-us/library/dd163157.aspx

        Thank you.

      • Bruno de Souza MeloBruno de Souza Melo commented  ·   ·  Flag as inappropriate

        Very useful!!

        I agree it would be nice to have a way to eager load on a navigation property in a derived type. Here is some tentative syntax for it with lambda expressions:

        var peopleAndManagers1 =
        from p in context.People.Include(p => (p as Employee).Manager)
        where p.LastName.StartsWith("a")
        select p;

        Or alternatively:

        var peopleAndManagers2 =
        from p in context.People.Include<Employee>(e => e.Manager)
        where p.LastName.StartsWith("a")
        select p;

      • rbetmrbetm commented  ·   ·  Flag as inappropriate

        Having the ability to eager load derived properties (provided you know and supplie their names) would be very useful. Unfortunately, I can't vote for this suggestion as I don't agree with the IncludeAll...

      • rbetmrbetm commented  ·   ·  Flag as inappropriate

        Eager loading is something that can be done on deep levels. For instance, on an Order object, I can eager load with:
        .Include("Lines.Product")
        Eager loading 'all properties' a single level deep does not make sense if you don't know the property names, because you then also don't know the semantics of the property and whether or not it makes sense to eager load only 1 level deep. For instance, order lines, which could be useless if the related product is not loaded.
        Eager loading All to a given level, as i.e. in:
        Context.OrderSet.IncludeAll(2)
        to load 2 levels deep is also very arbitrary AND very dangerous! It would load the Order's customer AND ALL to the customer related entities (it's other orders, it's login history records, etc.)
        Recursive eager loading (with infinite or unlimited depth) is dangerous and absurd. It would load the order, orderlines, customers, products, orderlines of all other orders, orderlines all my ordered products are on, all other customers, all suppliers,... the whole database!!!
        Except for very ad-hoc cases, I don't see a logical reason to support an IncludeAll operation.

      Feedback and Knowledge Base