Search for existing 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:

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).

69 votes
Sign in
Sign in with: facebook google
Signed in as (Sign out)

We’ll send you updates on this idea

Daniel Cazzulino shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →


Sign in
Sign in with: facebook google
Signed in as (Sign out)
  • Brian 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.

  • Brian 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 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.

  • mabster 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