Entity Framework Feature Suggestions

Mechanism for statically defining eager loading for navigation properties

Some times, depending on how you design your model, you want a certain navigation property to be loaded *always*. It's static part of the model just like the (implicit) lazy load nature of a virtual navigation property when lazy loading is configured.

There should be a way to define eager load for navigation properties, something like:

[Include]
public ICollection<Item> Items { get; set; }

and/or via DbModelBuilder:

modelBuilder.Entity<Order>().Property(x => x.Items).Include();

This would greatly simplify client code as it wouldn't have to include on each query/access, and it would also work with Find(key). This would make it more suitable to DDD-style design of entities (so called aggregate roots).

117 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…)
    Daniel CazzulinoDaniel Cazzulino shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →
    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.

    4 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...
      • BrianBrian commented  ·   ·  Flag as inappropriate

        Thinking about this a bit more...

        1. I'd like to eager load aggregate roots so that I can guarantee they are consistent.

        2. For navigation properties between aggregate roots I'd like to choose eager load vs lazy load based on performance needs.

        The first point is critical, the second is nice to have.

      • BrianBrian commented  ·   ·  Flag as inappropriate

        I'd prefer a global option to use eager loading across all entities (Context option or perhaps via conventions when they return).

      • Ladislav MrnkaLadislav Mrnka commented  ·   ·  Flag as inappropriate

        @mabster: In such case it is a time to define reusable extension method for your context which will wrap your eager loading call ...

        I don't say that possibility to statically define eager loading is not useful - for example last paragraph in proposal makes it valid requirement but some scenarios can be simply solved by common development techniques without any change to framework.

      • mabstermabster commented  ·   ·  Flag as inappropriate

        Yes! Every time I use EF and find myself writing .Include("Items") (or whatever) on every query, I wonder why this isn't possible. I love the idea of an Include method on the fluent interface. Feels right.

      Feedback and Knowledge Base