Eager Loading using Include & ThenInclude in EF Core

In this article let us explore the Eager Loading in EF Core using theInclude & ThenInclude method. The Include Lambda method is an extension method from the namespace Microsoft.EntityFrameworkCore.. We use the include & ThenInclude methods, along with the Projection Query in EF Core to load the related entities. In this tutorial, we look at include method and learn how to load entities from multiple levels and multiple tables. We also show how to filter, use them multiple times to load the related data.

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

Eager Loading in EF Core

Eager loading is a technique where EF core loads the related entities along with the main entity. All entities are loaded in a single query to database thus saving bandwidth and crucial server CPU time. The other two ways of loading data are Lazy Loading & Explicit Loading. The Eager Loading is in EF Core done using the Include & ThenInclude method

Include Method

Consider the following query which loads the customer into the context. We would also like to know the InvoiceDate & total from the Invoice table. One way is to handle it is by using the Lazy Loading in EF Core. The Other way is to use the include method.

To include the Invoice table, we use the Include method and passing the navigation property name as the lambda expression as shown below Include(c => c.Invoice). Note that the Invoice property is a collection navigational property in the customer entity.

You can look at the SQL query. The EF Core Left Joins the Invoice table to customer table correctly using the CustomerId as join condition.

The list of invoices are correctly returned as a collection. You can now loop through it and display it as shown below.

ThenInclude

You can drill down through relationships to include multiple levels.

For Example in the above query, we included invoice entity which has a one to many relationships with customer entity. We also have a Invoiceline, which has one to many relationships with the invoice entity. To load the Invoiceline we use the ThenInclude method on the invoice and passing InvoiceLine as lambda expression as shown below.

The data returned now has InvoiceLine collection under each Invoice.

Multiple Levels

The above query returned the invoiceLine , which has TrackId,Quantity & UnitPrice properties. To get the Track.Name, we need to include the Track entity. And Again to include MediaType.Name we need to include the MediaType entity

You can keep going down the Relationship chain using the ThenInclude as shown below

Multiple Includes

The Customer entity also related to Employee entity using the navigational property SupportRepId. We can start second Include to include that also in the result as shown below

Note that Include always applied on the first entity in the query (i.e Customer)

Filtered Include

The EF Core 3.0 Currently does not support adding Filters in Include/ThenInclude methods. But this feature will available in the next version of EF Core. You can use the following operators

  • Where,
  • OrderBy(Descending)/ThenBy(Descending),
  • Skip,
  • Take.

The operators applicable only on Collections. You can apply only one filter per navigation.

If you have included the navigation multiple times you should ensure that the filter is applied only once. Alternatively you can apply the same exact filter in all navigation

You can also use the where clause to filer out the result in the ThenInclude method.

Multiple ThenInclude

Now, Consider the following model. The child has two collection navigation properties. SubChild1 & SubChild1.

You can use multiple Includes on the same table Child and use the ThenInclude to load the SubChild1 & SubChild2. Note that if you are using the where clause in Include ensure that you use it on only one of them. Or use the same clause both of them. Different clauses will result in an error or may result in an invalid result.

The following is another example

Reference

Summary

The eager loading in EF Core done via the Include & ThenInclude methods. We need to supply the navigational property of the related entity as the argument. The next version of EF Core will also support the filtering & Ordering of the related data. You can apply on multiple tables and at multiple levels etc.

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