Roadmap › Entity Framework (EF6 & EF Core) › Module: Performance and Advanced
Lessons in this course (15)
13. EF Core performance checklist
Advanced★ Must knowLesson 13 of 15
- Project with
Select— fetch only needed columns. AsNoTrackingfor read-only queries.- Avoid N+1 — no lazy loading in loops;
Includeor projection. AsSplitQueryfor multiple collection includes (cartesian explosion).- Paginate; never load whole tables.
- Indexes for filters/joins/order-bys (
HasIndex). ExecuteUpdate/ExecuteDeletefor set-based changes.AddDbContextPooland compiled queries (EF.CompileAsyncQuery) for hot paths.- Keep contexts short-lived; don't track thousands of entities (DetectChanges cost).
- Check generated SQL (
ToQueryString, logging) and execution plans; drop to Dapper/raw SQL for complex reports. - Avoid client-side
ToList()beforeWhere. - Use
AnyAsyncfor existence,CountAsynconly when needed.
csharp
private static readonly Func<AppDbContext, int, Task<Product?>> GetById =
EF.CompileAsyncQuery((AppDbContext db, int id) => db.Products.FirstOrDefault(p => p.Id == id));