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)
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
***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
Last updated
Was this helpful?