---
description: >-
  In this field, i aim to explain what the lazy & eager loading are and what the
  differences of them are.
---

# Lazy Loading vs Eager Loading

First of all lazy loading and eager loading are  loading method by using query by using entity framework.



## Eager Loading

&#x20;      Eager loading is a technique where EF loads the related entities along with the main entity.All entities are loaded in a single query to database thus saving bandwidth and crucial server CPU time.This is done using "include method", which has two overloads.One of which takes navigation property as a string.the other "include method" is an extension method and far more flexible.In this tutorial,we learn how to make load the entities eagerly.We also show how to Eager Loading from multiple Levels and multiple Tables. &#x20;

&#x20;       One of the ways we can load entities eagerly by using the "include" method.Entity Framework creates a join query,when it sees the "include" method , thus bringing all the records in one single query.

&#x20;       There are two versions of the include method available. The default method where you need to specify the navigational Property as a string.\
&#x20;        The other one from the System.Data.Entity namespace.It takes a lambda expression,where you need to specify the navigational Property.



#### &#x20;  Include Default Method:

&#x20;          The following example uses the default method.As you can see we use ".Include ("ProductModel") on the Products model.The ProductModel is the name of the Navigational Property (!!!Not the name of the entity)

```
using (AdventureWorks db=new AdventureWorks())
//*****Disable Lazy Loading***///
db.Configuration.LazyLoadingEnabled=false;

//Log SQL command to Console;***Add windows form here.

db.Database.Log=Console.Write;
 var product = (from p in db.Products.Include("ProductModel")
                where p.ProductID==814
                select p).ToList();
 foreach (var p in product)
 {
 this is for console///Console.WriteLine("{0} {1} {2},p.ProductID,p.Name,p.ProductModel.Name);
 Messagebox.show("{0} {1} {2},p.ProductID,p.Name,p.ProductModel.Name);
 }                     
```

#### &#x20;   Include Lambda Method:

&#x20;            The include method above takes the string as the input parameter.This can be little inconvenient as no compile-time errors are thrown in case any spelling mistakes .Hence it is advisable to use the include extension method from the namespace System.Data.Entity

```
Using System.Data.Entity; /// First import this.

var product =(from p in db.Products.Include(p=>p.ProductModel)
             ///Using Lambda Expression Instead of a string
             where p.ProductID==814
             select p).ToList();
             
```

\*\*\*The method Include then is introduced in Entity Framework Core,It is not available in Entity Framework 6.

referencess:tektutorialshub.com,\
&#x20;                      medium.com/Engin Karabudak,\
&#x20;                      c-sharpcorner.com



