Provide mechanism to statically define 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).
4 comments
-
Brian
commented
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
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
@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
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.