Note: this article is published on 06/21/2024.
In this series of articles, we discuss Lazy Loading and related. There is no Lazy Loading Category on this site; we put the articles in the LINQ category because the behavior of Lazy Loading is based on or related to LINQ.
- Lazy Loading (0): Overview
- Lazy Loading (1): Difference Between IEnumerable and IQueryable
- Lazy Loading (2): HTML
- Lazy Loading (3): JavaScript
- Lazy Loading (4): EntityFramework --- this article
- Lazy Loading (5): Angular --- not done
Introduction
This article will discuss Lazy Loading for JavaScript and give a demo to demonstrate the Lazy Loading that will enable the images to load as the site visitors scroll down the page decreasing the page load time. The content of the article is
- Introduction
- A - Lazy Loading for C# Entiry Framework
- B - Demo Environment Setup
- C - Demo
- Lazy Loading
- Eager Loading
- Explicitly Loading
- D - Turning Off Lazy Loading
- E - Performance Consideration
A - Lazy Loading for C# Entity Framework
In this series of articles, in the previous two articles, we discussed lazy loading for HTML and JavaScript. They are both for loading content, such as photos. In this article, we discuss the Entity Framework Lazy loading, which is most likely to deal with database loading.
Lazy Loading for entity framework means the related entities are not loaded, until we iterate through them or bind them the data. By default, LINQ to SQL loads the related entities using Lazy Loading.
In this database table, there is a one-to-many relationship between Department and Course entities. A Department can have 1 or more courses open. When the departments are loaded, the related entities (Course entities) are not loaded. Course entity is only loaded when we iterate through the objects belonging to a given department.
B - Demo Environment Setup
In the database ContosoUniversity, Table Department and Table Course are one too Many relationships:

This is the Department Table:

This is the Course Table:

We make an entity to examine the relationship between these two tables:
For a .NET Framework project: right Click Project => Add New Item => Data => ADO.NET Entity Data Model

In the Entity Data Model Wizard: Click EF Designer from the Database.

Choose data source (connection):

Choose tables: Department & Course:

Get Entity Diagram:

C - Demo
There are several ways that the Entity Framework can load related data into the navigation properties of an entity:
-
Lazy loading. When the entity is first read, related data isn't retrieved. However, the first time you attempt to access a navigation property, the data required for that navigation property is automatically retrieved. This results in multiple queries sent to the database — one for the entity itself and one each time that related data for the entity must be retrieved. The
DbContextclass enables lazy loading by default.
When code runs at Line 36, Query all Department rows:

we may detect it by running SQL Profiler at Line37 (after LIne 36): only Department Tables were SELECT:

while after running Line 38 (say, the first loop), the SQL Server Profiler will detect the Course Table SELECT for specific Department ID --- the result is deferred from getting the Department table

This process is equivalent to the following:

-
Eager loading. When the entity is read, related data is retrieved along with it. This typically results in a single join query that retrieves all of the data that's needed. You specify eager loading by using the Include method.
When code runs at Line 36, Query all Department rows and related Course rows:

The SQL Server Profiler will show the result like this

This process is equivalent to the following:

-
Explicit loading. This is similar to lazy loading, except that you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. You load related data manually by getting the object state manager entry for an entity and calling the Collection.Load method for collections or the Reference.Load method for properties that hold a single entity. (In the following example, if you wanted to load the Administrator navigation property, you'd replace
Collection(x => x.Courses)withReference(x => x.Administrator).) Typically you'd use explicit loading only when you've turned lazy loading off.
The process is exactly the same as lazy loading, except the loading at Lione 57 is explicitly triggered by the Load() Method:

The following in SQL is the same as lazy loading:
at Line54 (after LIne 54): only Department Tables were SELECT:

while after running Line 57 (say, the first loop), the Course Table SELECT for specific Department ID by explicityly loading:

Because they don't immediately retrieve the property values, lazy loading, and explicit loading are also both known as deferred loading.
D - Turning Off Lazy Loading
As we mentioned previously:
-
By default, LINQ to SQL loads the related entities using Lazy Loading.
-
For the Entity Framework, The
DbContextclass enables lazy loading by default.
Now we discuss how to disable lazy loading, such as before serialization
- you can disable lazy loading and avoid proxy issues by disabling proxy creation.
public class BloggingContext : DbContext
{
public BloggingContext()
{
this.Configuration.ProxyCreationEnabled = false;
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
- Here are some other ways to disable lazy loading:
- For specific navigation properties, omit the
virtualthe keyword when you declare the property. - For all navigation properties, set
LazyLoadingEnabledtofalsePut the following code in the constructor of your context class:
- For specific navigation properties, omit the

Join the conversation! Your thoughts help the community grow.