Load Related Entities in Entity Framework

In this tutorial let us look into how to Load Related Entities in Entity Framework. We can load Related Entities either eagerly, lazily or explicitly. By default the EF loads the entities lazily. But you can instruct it load eagerly using the include method. You can make use of projection queries to load the data. The other option is to load the data explicitly using the load method of the context. In the explicit method, we decide when to load the data.

Load Related Entities

LINQ to Entities allows you to get related entities from multiple tables using navigational properties. In Entity Framework, we use the Navigation Properties to identify each end of the association. This was explained in our tutorial Relationship in Entity Framework. There are three kinds of relationships are possible One to One relationship, One to Many relationships  and Many to Many Relationship

You can refer to the following tutorial, to learn how to setup relationships in EF.

We continue to use the AdventureWorks Database for our model. If you missed the previous tutorials you can go through those from the following the LINQ to Entities tutoral

Loading the Related Data

There are several ways in which you can load the data from the related tables. All those methods fall into three categories

  1. Lazy Loading,
  2. Eager Loading.
  3. Explicit Loading

Lazy Loading

One way to retrieve the data is to load the related data, only when you actually need it. This technique is known as Lazy Loading. You can read more about Lazy Loading in Entity Framework

Example

The Product model in the AdventureWorks database has a One to Many relationship with the ProductModel model. The product belongs to only one Product model. The Product Model can have many products.

The querying for the Product is as shown below

The code above retrieves all the Products which starts with A. Note that we have not queried for the name of the Product Model. If you look at the SQL, it is a simple Select Query.

Inside the for loop, we have accessed the ProductModel.Name. At that point, Entity Framework sends the query to the database to retrieve the ProductModel. We call this behavior as Lazy Loading in Entity Framework. The Query is as shown below. Note that EF fires this query for each iteration.

Example

The Relationship between Person and email address is one to many. One person can have many email addresses. In the following example, we send the query for the Person entity.

The EF sends the following Query to the Server. Note that EF does not sends the query for the Email Address.

In the above example retrieves all the persons with the first name KEN. We loop through each person to access their email address. Then we loop through an email address collection to get the email address of each person.

The EF sends the query for the email address as we access the email address foreach (var e in p.EmailAddresses), It only retrieves the email of one person at a time. The no of queries that EF Sends to the database is equal to the no of persons. The following shows the SQL Query that retrieves the email address.

The above query in method syntax.

Eager Loading

We can load the related entities along with the main entity. We call this technique as Eager Loading. The Lazy Loading fires a query as we access each property of the related entity. This may flood the database with several queries. In such circumstances, it is better to get all the related data ahead of time with one query to save bandwidth and crucial server resources.

Include Method

There are two versions of the Include method available. Use the Include method from the System.Data.Entity namespace. So make sure you are using that namespace.
The other one the default method where you need to specify the navigational Property as a string

We can use the Include method to load the related entities along with the main query. The include method asks the entity framework to load the related entities at the time of retrieving the main entity.

Example of Include Method

The following examples show how to use the include method to load the Entities Eagerly. Include method takes the name of the navigational Property as shown below. The email address is queried along with the main entity as shown in the SQL Query

SQL Query of the above method

Both the above query uses the include method. Include method takes the name of the navigational Property (not the name of the related entity).

Projection

Projection queries in Entity Framework in another way to load the related data. When we use the projection Entity Framework will use the join query to get the related data.

For Example, consider this query. which queries for the Products. As you loop through the products and refer to p.ProductModel.Name the EF will send the query to the database to retrieve it. This is lazy loading.

But if we use the projection Query in Entity Framework to get only the columns we want (including the ProductModel) as shown below., then the EF will load the ProductModel when it retrieves the Products

The Corresponding Query is as shown below. Note that Query retrieves all the fields of ProductModel along with the ProductID & Name of the Product.

Here is another example of projection query where we use it get the collection of email addresses. Note that In the previous query a Product is associated with only one ProductModel. But in this case Person can have multiple email addresses. The EF is smart enough to know that and converts the returned data into collection of email addresses.

Explicit Loading

Explicit Loading is another way to load the related data. Here we make a explicit call the Load method to load the related data.

First, we need to disable the Lazy Loading

db.Configuration.LazyLoadingEnabled = false;

Use the Entry method on the entity and use the Reference method and call the Load on that related entity, which you want to load. In case of collections use the collection method instead of Reference method

db.Entry(p).Reference(m => m.ProductModel).Load();

Summary

In this article, we showed you how to load the related data using various methods like lazy loading, eager loading with projection query and include method. Explicit loading with the load method.

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