Eager, Lazy & Explicit Loading
Control when related data is loaded using Include, ThenInclude, lazy loading proxies, and explicit Load calls.
Three Loading Strategies
EF Core offers three strategies for loading related data: Eager Loading (load together with query), Lazy Loading (load on access), and Explicit Loading (load on demand). Choosing the right one avoids the N+1 problem and unnecessary database roundtrips.
Eager Loading with Include
Eager loading uses Include() to JOIN related data in the same SQL query. This is the most predictable strategy and avoids N+1 issues.
// Single query with JOIN
var blogs = await _db.Blogs
.Include(b => b.Posts)
.Where(b => b.IsActive)
.ToListAsync();
// SQL: SELECT * FROM Blogs LEFT JOIN Posts ON ...All lessons in this course
- Eager, Lazy & Explicit Loading
- Raw SQL, Stored Procedures & Interpolation
- Compiled Queries & Performance
- Global Query Filters & Owned Entities