Please enable Javascript to correctly display the contents on Dot Net Tricks!

Difference between Lazy Loading and Eager Loading

  Author : Shailendra Chauhan
Updated On : 21 Sep 2013
Total Views : 70,958   
Support : All
 

In LINQ and Entity Framework, you have Lazy Loading and Eager Loading for loading the related entities of an entity. In this article you will learn the differences between these two loading.

Lazy/Deferred Loading

In case of lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested. By default LINQ supports lazy loading.

For Example:

var query = context.Categories.Take(3); // Lazy loading

foreach (var Category in query)
{
 Console.WriteLine(Category.Name);
 foreach (var Product in Category.Products)
 {
 Console.WriteLine(Product.ProductID);
 }
 }

Generated SQL Query will be:

SELECT TOP (3) 
[c].[CatID] AS [CatID], 
[c].[Name] AS [Name], 
[c].[CreatedDate] AS [CreatedDate]
FROM [dbo].[Category] AS [c]
GO

-- Region Parameters
DECLARE @EntityKeyValue1 Int = 1
-- EndRegion
SELECT 
[Extent1].[ProductID] AS [ProductID], 
[Extent1].[Name] AS [Name], 
[Extent1].[UnitPrice] AS [UnitPrice], 
[Extent1].[CatID] AS [CatID], 
[Extent1].[EntryDate] AS [EntryDate], 
[Extent1].[ExpiryDate] AS [ExpiryDate]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[CatID] = @EntityKeyValue1
GO

-- Region Parameters
DECLARE @EntityKeyValue1 Int = 2
-- EndRegion
SELECT 
[Extent1].[ProductID] AS [ProductID], 
[Extent1].[Name] AS [Name], 
[Extent1].[UnitPrice] AS [UnitPrice], 
[Extent1].[CatID] AS [CatID], 
[Extent1].[EntryDate] AS [EntryDate], 
[Extent1].[ExpiryDate] AS [ExpiryDate]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[CatID] = @EntityKeyValue1
GO

-- Region Parameters
DECLARE @EntityKeyValue1 Int = 3
-- EndRegion
SELECT 
[Extent1].[ProductID] AS [ProductID], 
[Extent1].[Name] AS [Name], 
[Extent1].[UnitPrice] AS [UnitPrice], 
[Extent1].[CatID] AS [CatID], 
[Extent1].[EntryDate] AS [EntryDate], 
[Extent1].[ExpiryDate] AS [ExpiryDate]
FROM [dbo].[Product] AS [Extent1]
WHERE [Extent1].[CatID] = @EntityKeyValue1

In above example, you have 4 SQL queries which means calling the database 4 times, one for the Categories and three times for the Products associated to the Categories. In this way, child entity is populated when it is requested.

You can turn off the lazy loading feature by setting LazyLoadingEnabled property of the ContextOptions on context to false. Now you can fetch the related objects with the parent object in one query itself.

context.ContextOptions.LazyLoadingEnabled = false;

Eager loading

In case of eager loading, related objects (child objects) are loaded automatically with its parent object. To use Eager loading you need to use Include() method.

For Example

var query = context.Categories.Include("Products").Take(3); // Eager loading

 foreach (var Category in query)
 {
 Console.WriteLine(Category.Name);
 foreach (var Product in Category.Products)
 {
 Console.WriteLine(Product.ProductID);
 }
 }

Generated SQL Query will be

SELECT [Project1].[CatID] AS [CatID], 
 [Project1].[Name] AS [Name], 
 [Project1].[CreatedDate] AS [CreatedDate], 
 [Project1].[C1] AS [C1], 
 [Project1].[ProductID] AS [ProductID], 
 [Project1].[Name1] AS [Name1], 
 [Project1].[UnitPrice] AS [UnitPrice], 
 [Project1].[CatID1] AS [CatID1], 
 [Project1].[EntryDate] AS [EntryDate], 
 [Project1].[ExpiryDate] AS [ExpiryDate]
 FROM (SELECT 
 [Limit1].[CatID] AS [CatID], 
 [Limit1].[Name] AS [Name], 
 [Limit1].[CreatedDate] AS [CreatedDate], 
 [Extent2].[ProductID] AS [ProductID], 
 [Extent2].[Name] AS [Name1], 
 [Extent2].[UnitPrice] AS [UnitPrice], 
 [Extent2].[CatID] AS [CatID1], 
 [Extent2].[EntryDate] AS [EntryDate], 
 [Extent2].[ExpiryDate] AS [ExpiryDate], 
 CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int) 
ELSE 1 END AS [C1]
FROM (SELECT TOP (3) [c].[CatID] AS [CatID], [c].[Name] AS [Name], [c].[CreatedDate] AS [CreatedDate]
 FROM [dbo].[Category] AS [c] ) 
AS [Limit1]
 LEFT OUTER JOIN [dbo].[Product] AS [Extent2] 
ON [Limit1].[CatID] = [Extent2].[CatID]) AS [Project1]
 ORDER BY [Project1].[CatID] ASC, [Project1].[C1] ASC

In above example, you have only one SQL queries which means calling the database only one time, for the Categories and the Products associated to the Categories. In this way, child entity is populated with parent entity.

What do you think?

I hope you will enjoy the tips while programming with LINQ and Entity Framework. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+