Lazy Loading in Entity Framework
ets write a simple query to fetch the top 20 employees from the Northwind database and count the number of orders placed by each employee.
NORTHWNDEntities context = new NORTHWNDEntities();
var employees = context.Employees.Take(20).ToList();
foreach (Employee employee in employees)
{
Console.WriteLine("Total number of order by employee : {0}", employee.Orders.Count);
}
We start off with creating the ObjectContext which is NORTHWNDEntities in our case. We filter the 20 employees using Take method on the employees entity collection. Then we loop over the individual employees and display the count of Orders to the console window.
By default Entity Framework 4 enables the Lazy Loading feature. Because of this there are multiple hits to the database server. The initial query which filters the 20 employees is executed when we start iterating over the loop. And then for each employee a separate query is executed within the foreach loop.
Turn-off Lazy Loading
We can turn off the lazy loading feature and fetch the related records in one query itself. This process is termed as eager loading. To turn off Lazy loading we can set LazyLoadingEnabled property of the ContextOptions oncontext to false.
context.ContextOptions.LazyLoadingEnabled = false;
Now if we run the earlier code, we get the count of orders as 0. This is because only the first query is executed.
Explicitly Include results
If we want the resultset to include the dependent records we need do explicitly tell the framework to include them along with the first query results.
var employees = context.Employees.Include("Orders").Take(20);
Use the Load Method to request for related records
Another alternative is to call the load method inside the loop to load the related records.
employee.Orders.Load();
Console.WriteLine("Total number of order by employee : {0}", employee.Orders.Count);
Please note that in this case we should not use the Include method mentioned earlier on the statement which filters the 20 employees. We would run the query similar to the first case and then use the load method to load related objects.
Conclusion
Lazy loading is helpful if we have a large object graph and we need to perform some in memory calculations one at a time. We should be careful while using Lazy loading with service layer. Since services use serialization to transfer data over the wire, lazy loading might increase the processing time during serialization. In such scenarios we should avoid using lazy loading.
Keep coding... :-)
NORTHWNDEntities context = new NORTHWNDEntities();
var employees = context.Employees.Take(20).ToList();
foreach (Employee employee in employees)
{
Console.WriteLine("Total number of order by employee : {0}", employee.Orders.Count);
}
We start off with creating the ObjectContext which is NORTHWNDEntities in our case. We filter the 20 employees using Take method on the employees entity collection. Then we loop over the individual employees and display the count of Orders to the console window.
By default Entity Framework 4 enables the Lazy Loading feature. Because of this there are multiple hits to the database server. The initial query which filters the 20 employees is executed when we start iterating over the loop. And then for each employee a separate query is executed within the foreach loop.
Turn-off Lazy Loading
We can turn off the lazy loading feature and fetch the related records in one query itself. This process is termed as eager loading. To turn off Lazy loading we can set LazyLoadingEnabled property of the ContextOptions oncontext to false.
context.ContextOptions.LazyLoadingEnabled = false;
Now if we run the earlier code, we get the count of orders as 0. This is because only the first query is executed.
Explicitly Include results
If we want the resultset to include the dependent records we need do explicitly tell the framework to include them along with the first query results.
var employees = context.Employees.Include("Orders").Take(20);
Use the Load Method to request for related records
Another alternative is to call the load method inside the loop to load the related records.
employee.Orders.Load();
Console.WriteLine("Total number of order by employee : {0}", employee.Orders.Count);
Please note that in this case we should not use the Include method mentioned earlier on the statement which filters the 20 employees. We would run the query similar to the first case and then use the load method to load related objects.
Conclusion
Lazy loading is helpful if we have a large object graph and we need to perform some in memory calculations one at a time. We should be careful while using Lazy loading with service layer. Since services use serialization to transfer data over the wire, lazy loading might increase the processing time during serialization. In such scenarios we should avoid using lazy loading.
Keep coding... :-)
Comments