wasAsked.

What is the difference between lazy loading, eager loading and explicit loading in Entity Framework?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Entity Framework Core, .NET

Community answers

Posted by wasAsked editorial collection

These are three strategies for loading related data (navigation properties) in Entity Framework. Example model public class Customer { public int Id { get; set; } public string Name { get; set; } = ""; public List<Order> Orders { get; set; } = []; } public class Order { public int Id { get; set; } public int CustomerId { get; set; } public Customer Customer { get; set; } = null!; } 1. Eager loading — load related data in the same query var customers = await db.Customers .Include(c => c.Orders) .ToListAsync(); - EF generates a JOIN (or split queries) and loads customers and their orders together. - Use ThenInclude for deeper levels, filtered includes (Include(c => c.Orders.Where(o => o.Total > 1000))), and AsSplitQuery to avoid cartesian explosion with multiple collections. - Best when you know in advance that you need the related data. 2. Lazy loading — load related data automatically when first accessed var customer = await db.Customers.FirstAsync(c => c.Id == 1); var count = customer.Orders.Count; // triggers a separate query at this moment - In EF Core, lazy loading is off by default. Enable it with the Microsoft.EntityFrameworkCore.Proxies package (UseLazyLoadingProxies, with virtual navigation properties) or by injecting ILazyLoader. - In EF6, lazy loading was on by default for virtual navigation properties. - Convenient, but risky: looping over many customers and touching Orders causes the N+1 query problem (one query for customers plus one per customer). It can also trigger queries during JSON serialization or after the context is disposed. 3. Explicit loading — load related data on demand, deliberately var customer = await db.Customers.FirstAsync(c => c.Id == 1); await db.Entry(customer).Collection(c => c.Orders).LoadAsync(); await db.Entry(order).Reference(o => o.Customer).LoadAsync(); Filtered explicit loading: await db.Entry(customer).Collection(c => c.Orders).Query().Where(o => o.Total > 1000).LoadAsync(); - You control exactly when the extra query runs; useful when related data is needed only under certain conditions. Comparison - When data loads: eager — with the main query; lazy — automatically on first access; explicit — when you call Load. - Number of queries: eager — one (or a few with split queries); lazy — potentially many (N+1 risk); explicit — one per Load call. - Control: eager is predictable; lazy is implicit and can surprise you; explicit is deliberate. - Setup in EF Core: eager needs Include; lazy needs proxies or ILazyLoader; explicit needs db.Entry(...).Load. A fourth, often best option: projection var summaries = await db.Customers .Select(c => new { c.Name, OrderCount = c.Orders.Count, Total = c.Orders.Sum(o => o.Total) }) .ToListAsync(); EF generates exactly the SQL needed and loads only the required columns — ideal for API responses and read-only screens. Recommendations for web APIs - Avoid lazy loading in web applications; it hides database calls and easily causes N+1 problems. - Prefer projections for reads, eager loading when you need entities with related data, and explicit loading for conditional needs. - Use AsNoTracking for read-only queries. - Monitor generated SQL (logging) to confirm the number of queries.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics