Eager Loading in Entity Framework

Eager loading is a technique where EF 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. This is done using the Include method, which has two overloads. One of which takes navigation property as a string. The Other Include method is an extension method and far more flexible. In this tutorial, we learn how to make load the entities eagerly. We also show how to Eager Loading from multiple Levels and multiple Tables.

Dangers of lazy loading

The following query retrieves list of all products from the database. In the for loop we access the p.ProductModel.Name property, which comes from the related navigational property ProductModel

Consider you have 100 Products in your Product table. When you iterate through over the Products collection and access the ProductModel, the EF will fire a query to retrieve the details of the ProductModel. That means EF sends 100 queries for each of those100 Products which is very inefficient.

We can solve the above problem by loading the ProductModel eagerly. along with the Products.using a Single Query.

Eager Loading in Entity Framework

One of the ways we can load entities eagerly by using the Include method. Entity Framework creates a join query, when it sees the Include method, thus bringing all the records in one single query.

The code below demonstrates the use of Include method. Disable Lazy loading before trying out any of the codes. You should also log database command to console to see the SQL command that Entity Framework sends to the database.

There are two versions of the Include method available.

The default method where you need to specify the navigational Property as a string

The other one from the System.Data.Entity namespace. It takes a lambda expression, where you need to specify the navigational Property

Include default method

The following example uses the default method. As you can see we use .Include("ProductModel") on the Products model. The ProductModel is the name of the Navigational Property (not the name of the entity).

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

The Corresponding SQL Query of the above method.

Include Lambda Method

The Include method above takes the string as the input parameter. This can be little inconvenient as no compile-time errors are thrown in case any spelling mistakes. Hence it is advisable to use the Include extension method from the namespace System.Data.Entity

The method IncludeThen is introduced in Entity Framework Core, It is not available in Entity Framework 6

Eager Loading from Multiple Tables

You can load multiple related tables by using the following syntax. In the code below we have two tables ProductModel and ProductSubcategory

The corresponding SQL Query.

Eager Loading Multiple Levels

You can also load entities to multiple levels using the following syntax. The include statement includes bothEmployee and Person table (note that both are reference types). The Query will use join to bring data from both the tables as shown below.

The following example Queries ProductVendors which is a collection along with the Product. To get the Vendor we can make use of the select statement. Take a look at the complex query the EF generates for this task.

SQL Query

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