Hi Team,
Today I will explain Lazy Loading and Eager Loading.
Firstly we will explore Eager Loading.
Eager Loading is the process of loading the related data. We will use Include keyword to load the related data.
Let us take an example of entity framework where we have two tables one is Categories(Parent) and another one is Products(Child) table.
In case of Eager Loading if we load the data using following query :-
var query = ctx.Categories.Include("Products"); // Eager loading
The above query will bring the Categories record and their related Products data.
While debugging if we explore the SQL query it uses then we can see it will form the following query :-
SELECT
[Project1].[CategoryId] AS [CategoryId],
[Project1].[CategoryName] AS [CategoryName],
[Project1].[C1] AS [C1],
[Project1].[ProductId] AS [ProductId],
[Project1].[ProductName] AS [ProductName],
[Project1].[CategoryId1] AS [CategoryId1]
FROM ( SELECT
[Extent1].[CategoryId] AS [CategoryId],
[Extent1].[CategoryName] AS [CategoryName],
[Extent2].[ProductId] AS [ProductId],
[Extent2].[ProductName] AS [ProductName],
[Extent2].[CategoryId] AS [CategoryId1],
CASE WHEN ([Extent2].[ProductId] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
FROM [dbo].[Category] AS [Extent1]
LEFT OUTER JOIN [dbo].[Product] AS [Extent2] ON [Extent1].[CategoryId] = [Extent2].[CategoryId]
) AS [Project1]
ORDER BY [Project1].[CategoryId] ASC, [Project1].[C1] ASC
As we can see from above query it will bring records from Categories as well as Products table.
In case of Lazy Loading it will load the data from Parent table and it will exclude the Child table.
If we take a query example of Lazy Loading as shown below :-
var query = ctx.Categories; //Lazy Loading
It will load the data using following SQL query :-
SELECT
[Extent1].[CategoryId] AS [CategoryId],
[Extent1].[CategoryName] AS [CategoryName]
FROM [dbo].[Category] AS [Extent1]
As we can see from above query it will hit only one table and bring down only Category Records.
When to use Eager Loading and Lazy Loading ?
Its answer is simple. We have to load the required object to improve the query performance or to minimize excessive hits in database. In case of Eager Loading requested objects are loaded and in case of lazy loading only Parent Object is loaded. Until or otherwise you have set Enable Lazy Loading property to true.
In the following screens i will show you how to set Enable Lazy Loading property :-
In the entity model right click the properties.You will see the properties screen as below :-
In the above screen You can Enable Lazy Loading by setting it to true. By default it is true.
I have implemented the following code to more clarify :-
public void LazyLoading()
{
using (var ctx = new TestDbEntities())
{
var query = ctx.Categories; //Lazy Loading
foreach (var Category in query)
{
//Category.Name;
foreach (var Product in Category.Products)
{
// Product.ProductID;
}
}
}
}
public void EagerLoading()
{
using (var ctx = new TestDbEntities())
{
var query = ctx.Categories; // Eager loading
foreach (var Category in query)
{
//Category.Name;
foreach (var Product in Category.Products)
{
//Product.ProductID;
}
}
}
}
For further reference you can go to following link :-
https://msdn.microsoft.com/en-in/data/jj574232.aspx
Thanks
0 Comment(s)