articles

Home / DeveloperSection / Articles / lazy loading in entity framework

lazy loading in entity framework

lazy loading in entity framework

Anonymous User 1353 01-Dec-2020

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 EmployeeDBEntities())

            {
                //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 EmployeeDBEntities : DbContext
    {
        public EmployeeDBEntities()
            : base("name=EmployeeDBEntities")
        {
            this.Configuration.LazyLoadingEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

    }
}

Rules for lazy loading: 

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

I hope it will help to you.


I am a content writter !

Leave Comment

Comments

Liked By