articles

Home / DeveloperSection / Articles / Lazy loading vs Eager loading

Lazy loading vs Eager loading

Anchal Kesharwani9932 25-Jun-2014

In this article describe the concept of lazy loading vs eager loading in c#. Here we also describe a suitable example of lazy loading and eager loading. 

Eager Loading

In Eager Loading, all relevant data for an entity is loaded at the time of the query of the entity in the context object. Eager Loading can be done by using the "Include" method. To perform Eager Loading, Lazy Loading must be disabled. Lazy Loading is enabled by default in Entity Framework.

Example

Suppose I have the three tables DepartmentMaster, EmployeeMaster and EmployeeDetail. The relation among these three tables is shown in the following diagram.

Lazy loading vs Eager loading

using (var context = new MyContext())

 

            {

                Console.WriteLine("Eager Lading Example....");

                var dep = context.DepartmentMaster.Include("EmployeeMaster.EmployeeDetail").Where(f => f.DepartmentID == 1).FirstOrDefault();

                foreach (var emp in dep.EmployeeMaster)

                {

                    Console.WriteLine("Code: " + emp.Code + " Email Address:" + emp.EmployeeDetail.PersonalEmail);

                }

                Console.ReadKey();

 

            } 

Lazy Loading

Lazy loading is also known as deferred loading. Lazy loading is a technique, pattern where a data is loaded only on demand or only when it is required in order to increase the program efficiency, performance, etc.Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. Lazy loading is also called deferred loading or on-demand loading. It is called so because, usually the data for an object will be loaded whenever it is initialized but in this technique the loading is postponed or deferred till there is a requirement for the data. This is done when the data is really big or involves some sort of complexities in loading and it may not be used frequently.

In some cases, users may not be interested to view entire data too. Hence, loading entire data is not that great idea in these scenarios. In these cases, one can decide to lazy load the data depending upon their situations for gaining better performance, etc.

It is only used when you only need the main entity data to be fetched and you know that the related data will not be required.

using (var context = new MyContext())

 

            {

                //With Object Context

 

                context.ContextOptions.LazyLoadingEnabled = true;

 

                //With DB Context

 

                //context.Configuration.LazyLoadingEnabled = true;

 

 

 

                Console.WriteLine("Lazy Lading Example....");

 

                var dep = context.DepartmentMaster.Where(f => f.DepartmentID == 1).FirstOrDefault();

 

                foreach (var emp in dep.EmployeeMaster)

                {

 

                    Console.WriteLine("Code: " + emp.Code + " Email Address:" + emp.EmployeeDetail.PersonalEmail);

 

                }

 

            }


Updated 04-Apr-2020

Leave Comment

Comments

Liked By