Explicit Loading in Entity Framework Core

Explicit Loading in EF Core is a technique we query and load the related entities with an explicit call. Explicit loading works very similar to Lazy Loading, but the loading of the related entities happens only after an explicit call to the Load or Query method of the related entity’s DbContext.Entry(...) API object. In eager loading, we query the related entities along with the main entity in a single Query. In Lazy loading, EF loads the load related entities whenever we access the related Property.

Database:
The Database for this tutorial is taken from the chinook database.
Source Code:
The source code of this project available in GitHub. It also contains the script of the database

Explicit Loading in Entity Framework Core

To make an explicit call to load a related entity, we use the Load method of the related entity’s DbContext.Entry(...)object.

To Invoke load method, first we use the DbContext.Entry method is used to gain access to a tracked entity. From the Entry, we can use the Reference or Collection method to get the reference to the navigational property. Then you can call the Load method to get the data from the database.

In the following example query, we retrieve five Tracks from the database. Inside the for loop, we first get the DbContext.Entry of the Track entity db.Entry(track)

Now, we can get the reference to the Album using the reference method.db.Entry(track).Reference(t => t.Album)

Finally, use the Load method (db.Entry(track).Reference(t => t.Album).Load();) to send the query to the database and load the related Album data.

Now, since the album is loaded, we can access its Entry method (db.Entry(track.Album)) and use it load the Artist entity. (db.Entry(track.Album).Reference(t => t.Artist).Load();)

The corresponding SQL

Loading Collections

We use the reference method if the navigation property is of reference type. In the case of collection navigation property, the collection method is to be used

The Trackis a collection navigation property the entity Album. The following example retrieves the Albumand then uses the load method to explicitly load the Tracks that belong that album. Since it is a collection navigation property we make use of the Collection method

The SQL Query

Explicit Loading with Filtering

Unfortunately you cannot use the filter while using the Load method. For Example the following query results in an error.

To filter out the result of explicit loading, you can use the Query method. The query method returns the instance of IQueryable (or Query Variable). Hence you need to call Load() (or ToList(), FirstOrDefault() etc) to execute the query.

Virtual Property

You do not have the navigation properties as virtual.for the explicit loading to work.

References

Summary

Explicit Loading uses the Load or Query method of the DbContext.Entry(...) APIto load related data. Here you have full control over how & when the query is sent to the database. You can also filter the query using where before sending it to the database using the Query method.

2 thoughts on “Explicit Loading in Entity Framework Core”

  1. I have a solution I am converting from .NET Core 2.2 to 3.1. I have upgraded my Entity Framework packages. However, when trying to use .Collection() or .Reference() after loading the entry, I can only pass a string to these methods. An overload to pass in a lambda is not available. Is there an extension package I need to add to my project to get the lambda versions of .Collection() and .Reference()?

    This is the code I am using that builds:
    var item = _dbContext.Users.First();
    var entry = _dbContext.Entry(item).Collection(“Address”);

    What I am trying to use but get an error in VS Code that it cannot convert a lambda expression to a string:
    var item = _dbContext.Users.First();
    var entry = _dbContext.Entry(item).Collection(x => x.Address);

    1. Check if you also updated correctly to EF Core & EF Core SQL server to 3.1.3.
      No Need for any extension package

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top