Update: this is now implemented in Entity Framework Core 1.1: https://github.com/aspnet/EntityFramework/releases/tag/rel%2F1.1.0.
This is the second in a series of posts about missing functionality in EF Core. You can find the first here.
Entity Framework used to support three ways to load related entities (one to one, one to many, many to one, many to many):
Explicit loading: introduced in the first version of EF, required an explicit method call to DbEntityEntry<TEntity>.Collection<TElement>.Reload() or DbEntityEntry<TEntity>.Reference<TElement>.Reload() (in Code First), and wasn’t that popular, IMO;
Eager loading: related entities and collections would be fetched at the same time as their containing entity, through a call to one of the Include methods;
Lazy loading: entities would be loaded when and if they declaring property was accessed.
Entity Framework Core doesn’t include (yet) the explicit loading and lazy loading mechanisms. Lazy loading is not included because EF Core does not generate proxies for the entities it loads, and I guess nobody bothered to implement explicit loading. Well, that’s exactly what we’ll do here!
The idea is, even if we didn’t require a collection to be loaded when we issued the query, we can still load it afterwards. We should keep a syntax similar to the previous version, when this feature was available:
We will create an extension method over EntityEntry<T>:
The GetEntityKey method was introduced in the previous post, but I will add it here, for your convenience:
You can see that the Load method takes two parameters: the first is required, it represents the collection that we with to load, the second is optional, and it represents the go-back property, in case there is more than one property of the root entity’s type. For example:
In this case, the path p => p.Blog is needless, because a Post only belongs to one Blog.
A similar approach can be used to load explicitly other kinds of relations (one to one and many to one).
Hope you find this useful!