Introduction:

 

In this article we will explain what is lazy loading in Entity Framework or what is benefits of lazy loading in Entity Framework or how to turn off lazy loading for a particular property or all entities in the context with example.

 

Description:

 

Entity Framework lazy loading is one of the important concepts. Lazy loading means delaying the loading of related data, until you specifically request for it or lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested.  For example, employee class contains employeeAddress as a complex property. So, the context first loads all the employee from the database, then it will load the address of a particular employee when we access employeeAddress property as shown below.

 

      using (var ctx = new ArticleDBEntities())
            {
                //Loading Employee only
                IList<Employee> empList = ctx.Employee.ToList<Employee>();
 
                Employee emp = empList[0];
 
                //Loads Employee address for particular Employee only (seperate SQL query)
                EmployeeAddress address = emp.EmployeeAddress.FirstOrDefault();
            }

 

The code shown above will result in two SQL queries. First, it will fetch all Employee:

SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[EmployeeName] AS [EmployeeName],
[Extent1].[ContactNo] AS [ContactNo],
[Extent1].[EmailID] AS [EmailID]
FROM [dbo].[Employee] AS[Extent1]

 

Then, it will send the following query when we get the reference of EmployeeAddress:

exec sp_executesql N'SELECT 
[Extent1].[ID] AS [ID],
[Extent1].[EmpID] AS [EmpID],
[Extent1].[Address] AS [Address]
FROM [dbo].[EmployeeAddress] AS [Extent1]
WHERE [Extent1].[EmpID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1


However, you can also turn off lazy loading for a particular property or an entire context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false:

namespace MVC_Tutorials
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    using System.Data.Objects;
    using System.Data.Objects.DataClasses;
    using System.Linq;
   
    public partial class ArticleDBEntities : DbContext
    {
        public ArticleDBEntities()
            : base("name=ArticleDBEntities")
        {
            this.Configuration.LazyLoadingEnabled = false;
        }
   
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
   
        
    }
}

 

Rules for lazy loading:

  1. context.Configuration.ProxyCreationEnabled should be true.
  2. context.Configuration.LazyLoadingEnabled should be true.
  3. Navigation property should be defined as public, virtual. Context will NOT do lazy loading if the property is not defined as virtual.


  4. I hope it will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.