Click here to Skip to main content
12,889,448 members (48,699 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


5 bookmarked
Posted 23 Feb 2014

Deferred Execution Vs Lazy Loading Vs Eager Loading Vs Explicitly Loading

, 23 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
Deferred Execution Vs Lazy Loading Vs Eager Loading Vs Explicitly Loading

Deferred Execution - ”don’t compute the result until the caller actually uses it.”

Lazy loading and explicit loading are both types of deferred execution.

Lazy Loading - “don’t do the work until you absolutely have to.”

An entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. Related/child objects are not loaded automatically when a navigation property is accessed. When using POCO (Plain Old CLR Objects) entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

In case of Entity Framework, You can turn off the lazy loading feature by setting LazyLoadingEnabled property of the ContextOptions on context to false. This is very useful when you are sure that you will need child object as well. Lazy loading and serialization don’t mix well, and if you aren’t careful, you can end up querying for your entire database just because lazy loading is enabled. Most serializers work by accessing each property on an instance of a type. Property access triggers lazy loading, so more entities get serialized. On those entities, properties are accessed, and even more entities are loaded. It’s a good practice to turn lazy loading off before you serialize an entity.

context.ContextOptions.LazyLoadingEnabled = false;

When using POCO entity types, Lazy loading can be turned off by making the Posts property non-virtual:

public class Blog 
     public int BlogId { get; set; } 
     public string Name { get; set; } 

     public <del>virtual</del> ICollection<Post> Posts { get; set; } 

Eager Loading – “do all the work in advance”

A query for one type of entity also loads related/child entities as part of the query. Child objects are loaded automatically with its parent object when parent object is loaded.

In case of Entity Framework, You can achieve Eager loading by using ObjectQuery<T>.Include() method.

// Loads contacts, all related SalesOrderDetails
// Load all blogs, all related posts, and all related comments 
 var blogs1 = context.Blogs.Include(b => b.Posts.Select(p => p.Comments)).ToList();

Explicitly Loading – “do all the work even with lazy loading disabled”

Even with lazy loading disabled, it is still possible to lazily load related entities, but it must be done with an explicit call. To do so, you use the Load method on the related entity’s entry.

// Load the blog related to a given post 
context.Entry(post).Reference(p => p.Blog).Load();
// Load the posts with the 'entity-framework' tag related to a given blog 
     .Collection(b => b.Posts) 
     .Where(p => p.Tags.Contains("entity-framework") 

Using Query to Count Related Entities Without Loading Them

// Count how many posts the blog has, without fetching/loading all the posts
     .Collection(b => b.Posts) 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Adarsh Chaurasia - Enjoying Full Stack Development
Software Developer (Senior)
India India
 Innovative & tech-savvy software development professional wit7 6+ years of progressive experience specializing in object-oriented approach to enterprise commercial solution delivery utilizing latest technologies.
 Extensive background in Full Life-Cycle of Software Development and Automation process including requirements gathering, design, coding, unit testing, automation, debugging and maintenance.
 Comprehensive knowledge of Relational Database technology, queries and procedures.
 Excellent understanding of OOPS Concepts, Design Patterns & Best Coding Practices.
 Strong architectural knowledge of designing n-tier and n-layer solutions using Application blocks, Enterprise library, SOA, Software factories.
 Working experience of IIS administration and website deployment and configuration.

Strengths & Area of expertise include:

 Full Stack Enterprise Web Development
 Multi-Threaded Programming
 Object Oriented Development
 REST APIs Development & Integration
 Distributed/Client Server System
 TDD & Behavior Driven Development (BDD)
 Database Design & Development
 Exceptional Analytical & Quick Learning Skill
 Team & Client Communication
 Proactive & Organization Development Attitude

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
girishmeena28-Aug-16 4:36
membergirishmeena28-Aug-16 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 24 Feb 2014
Article Copyright 2014 by Adarsh Chaurasia - Enjoying Full Stack Development
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid