Tutorials Entity Framework Core Tutorial
Indexing for EF Core Applications
Indexing for EF Core Applications: free step-by-step lesson with examples, common mistakes, and interview tips — part of Entity Framework Core Tutorial on Toolliyo Academy.
On this page
Entity Framework Core Tutorial · Lesson 62 of 100
Indexing for EF Core Applications
Beginner ✓ → Intermediate ✓ → Advanced → Professional
Advanced · 3 — Production skills · ~10 min · Module 7: Performance Optimization
What is this?
Indexes speed lookups and sorts EF generates — define with HasIndex in Fluent API or migrations; align with Where, Join, and OrderBy columns.
Why should you care?
ShopNest filters Products by CategoryId and IsPublished on every browse — composite index turns scan into seek.
See it live — copy this example
Paste into a .NET project with EF Core packages, then run with LocalDB/SQL Server (dotnet ef / dotnet run).
modelBuilder.Entity<Product>(e =>
{
e.HasIndex(p => new { p.CategoryId, p.IsPublished, p.Name });
e.HasIndex(p => p.Sku).IsUnique();
});
// Query uses index when filtering matching columns:
await _context.Products
.Where(p => p.CategoryId == id && p.IsPublished)
.OrderBy(p => p.Name)
.ToListAsync();
What happened?
- Composite index on CategoryId, IsPublished, Name supports filter and sort.
- EF migrations emit CREATE INDEX; SQL Server optimizer uses it automatically.
Practice next
- Log slow queries and note WHERE/ORDER BY columns.
- Add HasIndex in Fluent API and migrate.
- Verify index usage in SSMS actual execution plan.
- Add filtered index WHERE IsPublished = 1.
- Drop unused index after monitoring sys.dm_db_index_usage_stats.
Remember
Indexes match EF query patterns. Configure via Fluent HasIndex. Validate with execution plans.
ShopNest browse index
DBA adds composite index after EF logs show table scan on category browse.
Outcome: CPU on SQL node drops 30% during weekend sale.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!