📈
Entity FrameWork(EF) Ado.Net
  • Entity FrameWork
  • ORM(Object Relational Mapping)
  • Lazy Loading vs Eager Loading
  • Dapper
Powered by GitBook
On this page

Was this helpful?

Lazy Loading vs Eager Loading

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

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

Eager Loading

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.

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.

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

Include Default Method:

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);
 }                     

Include Lambda Method:

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, medium.com/Engin Karabudak, c-sharpcorner.com

PreviousORM(Object Relational Mapping)NextDapper

Last updated 4 years ago

Was this helpful?