Entity Framework – Eager loading, Lazy loading



can any one please give more details about eager loading and lazy loading with difference ? can anyone provide simple sample how it works.

in progress 0
Technology Praansh 9 months 2 Answers 1064 views 0

Answers ( 2 )

  1. => Lazy loading :

    In this loading method, the child objects are not loaded with its parents by default, we have to make another request to get the child objects. So in the server, data execution (fetching data) will be happening again and again for every request, hence the speed of the application will be reduced.

    We can control these loading features by setting Context options in our dbContext class.

    context.ContextOptions.LazyLoadingEnabled = false;

    The above code will disable lazy loading and we will get the parent and child objects in a single query itself.

    LINQ queries support lazy loading by default.

    =>Eager Loading

    In this loading method, parent and child object loads in a single request .We have to use Include () method to get the related data of the parent. If we are sure about the data, then use the eager loading method by including the child property name to its parent.

    Var query = context. User.Include(“UserDetails “).Take(3);

    In the above query, you can see Include command, this query will fetch parent User as well as child UserDetail in a single hit.

Leave an answer


Captcha Click on image to update the captcha .

About Praansh