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)

Best Entity Framework Core Books
The Best EF Core Books, which helps you to get started with EF Core  

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.

Leave a Comment

Your email address will not be published.

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

Scroll to Top