Tuesday, 12 April 2016

Difference between Lazy Loading and Eager Loading

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:



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

  2.  

  3. foreach (var Category in query)

  4. {

  5. Console.WriteLine(Category.Name);

  6. foreach (var Product in Category.Products)

  7. {

  8. Console.WriteLine(Product.ProductID);

  9. }

  10. }

Generated SQL Query will be:



  1. SELECT TOP (3)

  2. [c].[CatID] AS [CatID],

  3. [c].[Name] AS [Name],

  4. [c].[CreatedDate] AS [CreatedDate]

  5. FROM [dbo].[Category] AS [c]

  6. GO

  7.  

  8. -- Region Parameters

  9. DECLARE @EntityKeyValue1 Int = 1

  10. -- EndRegion

  11. SELECT

  12. [Extent1].[ProductID] AS [ProductID],

  13. [Extent1].[Name] AS [Name],

  14. [Extent1].[UnitPrice] AS [UnitPrice],

  15. [Extent1].[CatID] AS [CatID],

  16. [Extent1].[EntryDate] AS [EntryDate],

  17. [Extent1].[ExpiryDate] AS [ExpiryDate]

  18. FROM [dbo].[Product] AS [Extent1]

  19. WHERE [Extent1].[CatID] = @EntityKeyValue1

  20. GO

  21.  

  22. -- Region Parameters

  23. DECLARE @EntityKeyValue1 Int = 2

  24. -- EndRegion

  25. SELECT

  26. [Extent1].[ProductID] AS [ProductID],

  27. [Extent1].[Name] AS [Name],

  28. [Extent1].[UnitPrice] AS [UnitPrice],

  29. [Extent1].[CatID] AS [CatID],

  30. [Extent1].[EntryDate] AS [EntryDate],

  31. [Extent1].[ExpiryDate] AS [ExpiryDate]

  32. FROM [dbo].[Product] AS [Extent1]

  33. WHERE [Extent1].[CatID] = @EntityKeyValue1

  34. GO

  35.  

  36. -- Region Parameters

  37. DECLARE @EntityKeyValue1 Int = 3

  38. -- EndRegion

  39. SELECT

  40. [Extent1].[ProductID] AS [ProductID],

  41. [Extent1].[Name] AS [Name],

  42. [Extent1].[UnitPrice] AS [UnitPrice],

  43. [Extent1].[CatID] AS [CatID],

  44. [Extent1].[EntryDate] AS [EntryDate],

  45. [Extent1].[ExpiryDate] AS [ExpiryDate]

  46. FROM [dbo].[Product] AS [Extent1]

  47. 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.

  1. 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



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

  2.  

  3. foreach (var Category in query)

  4. {

  5. Console.WriteLine(Category.Name);

  6. foreach (var Product in Category.Products)

  7. {

  8. Console.WriteLine(Product.ProductID);

  9. }

  10. }

Generated SQL Query will be



  1. SELECT [Project1].[CatID] AS [CatID],

  2. [Project1].[Name] AS [Name],

  3. [Project1].[CreatedDate] AS [CreatedDate],

  4. [Project1].[C1] AS [C1],

  5. [Project1].[ProductID] AS [ProductID],

  6. [Project1].[Name1] AS [Name1],

  7. [Project1].[UnitPrice] AS [UnitPrice],

  8. [Project1].[CatID1] AS [CatID1],

  9. [Project1].[EntryDate] AS [EntryDate],

  10. [Project1].[ExpiryDate] AS [ExpiryDate]

  11. FROM (SELECT

  12. [Limit1].[CatID] AS [CatID],

  13. [Limit1].[Name] AS [Name],

  14. [Limit1].[CreatedDate] AS [CreatedDate],

  15. [Extent2].[ProductID] AS [ProductID],

  16. [Extent2].[Name] AS [Name1],

  17. [Extent2].[UnitPrice] AS [UnitPrice],

  18. [Extent2].[CatID] AS [CatID1],

  19. [Extent2].[EntryDate] AS [EntryDate],

  20. [Extent2].[ExpiryDate] AS [ExpiryDate],

  21. CASE WHEN ([Extent2].[ProductID] IS NULL) THEN CAST(NULL AS int)

  22. ELSE 1 END AS [C1]

  23. FROM (SELECT TOP (3) [c].[CatID] AS [CatID], [c].[Name] AS [Name], [c].[CreatedDate] AS [CreatedDate]

  24. FROM [dbo].[Category] AS [c] )

  25. AS [Limit1]

  26. LEFT OUTER JOIN [dbo].[Product] AS [Extent2]

  27. ON [Limit1].[CatID] = [Extent2].[CatID]) AS [Project1]

  28. 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.

No comments:

Post a Comment