Lazy Loading in Entity Framework

It is opposite of eager loading.It delays loading process of related data.That means it won't load data until you request for it.
Using (var context=new CustomerDBEntities())
{
IList custlist=context.customers.ToList ();
customer cust=custlist[0];
//loads address for 0 index customer
customeraddress custaddr=cust.CustomerAddress;
}

you can disable Lazy loading by entering following command in constructor of DbContext class.
this.Configuration.LazyLoadingEnabled = false;

The navigation property should be defined as public and virtual. Note:-if property is not defined virtual then lazy loading will not fire.

public class Orderr
{
[Key]
public int OrderID { get; set; }
public string CustomerID { get; set; }
public string EmployeeID { get; set; }
public string OrderDate { get; set; }
public virtual ICollection   OrderDetails   { get; set; }
}
                    
public class OrderDetail
{
[Key]
public int orderid { get; set; }
public int productid { get; set; }
public int UnitPrice { get; set; }
public int Quantity { get; set; }        
public virtual Orderr Order { get; set; }
}
                    

SCENARIO 1: LAZYLOADING =TRUE


              
Console.WriteLine("LAZY LOADING IS TRUE WITH NO INCLUDE KEYWORD IN CONTEXT CLASS AND VIRTUAL KEYWORD IN PROPERTIES OF BOTH THE RELATED CLASSES");
foreach (var c in context.orderr)
{
Console.WriteLine(c.OrderDate);
foreach (var orderdet in c.OrderDetails)
{
    Console.WriteLine("Unitprice{0}", orderdet.UnitPrice);
}
Console.ReadLine();
}
                    

SCENARIO 2: LAZYLOADING = FALSE


              
Console.WriteLine("LAZY LOADING IS FALSE WITH INCLUDE KEYWORD IN CONTEXT CLASS AND VIRTUAL KEYWORD IN PROPERTIES OF BOTH THE RELATED CLASSES");
List ord = context.orderr.Include("orderdetails").ToList();
foreach (var c in ord)
{
Console.WriteLine(c.OrderDate);
foreach (var orderdet in c.OrderDetails)
{
    Console.WriteLine("Unitprice{0}", orderdet.UnitPrice);
}
Console.ReadLine();
}
                    

SCENARIO 3: LAZYLOADING = FALSE ,NO VIRTUAL KEYWORD IN BOTH CLASSES


              
Console.WriteLine("LAZY LOADING IS FALSE WITH INCLUDE AND NO VIRTUAL");
Console.WriteLine("OUTPUT NO DYNAMIC PROXIES DURING RUNTIME,BUT IT FETCHES DATA")
List ord = context.orderr.Include("orderdetails").ToList();
foreach (var c in ord)
{
Console.WriteLine(c.OrderDate);
foreach (var orderdet in c.OrderDetails)
{
Console.WriteLine("Unitprice{0}", orderdet.UnitPrice);
}
Console.ReadLine();
}
                    

Lazy Loading Configuration :


public class SampleDBContext :DbContext
{
public SampleDBContext():base("name=connectdb")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
this.Configuration.LazyLoadingEnabled = false;
}
public DbSet  orderr { get; set; }
}
                    

Turn Off Lazy Loading :


If you dont turnoff lazy loading,it loads with entire database records.

How to Turn Off Lazy Loading for Specific Property in an Entity:


In the following example, i dont want orderdetails to be lazyloaded.So how to turn of lazy loading for that specific property.


Note : Just remove "Virtual" keyword from OrderDetails collection.


public partial class Orders {
public Orders() {
this.Orderdetails = new HashSet
();
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public string EmployeeID { get; set; }
public string OrderDate { get; set; }
public ICollection
OrderDetails { get; set; }
}
                    

Turn Off Lazy Loading for all entities :


Lazy loading can be turned off for all entities in the sampledbcontext by setting a flag on the Configuration property to false as shown in the following example.

Note: this.Configuration.LazyLoadingEnabled = false;


public partial class SampledbContext : DbContext {
public SampledbContext(): base("name = SampledbContextentities") {
this.Configuration.LazyLoadingEnabled = false;
}
}
                

After turning off lazy loading, now when you run the above example again you will see that the Orderdetails are not loaded and only order data is retrieved.