Skip to main content

Entity Framework Core Best Practices for Production

February 28, 2026 2 min read

Introduction

Entity Framework Core is powerful but easy to misuse. After years of production experience, here are the best practices to follow on every project.

1. Always Use AsNoTracking for Read-Only Queries

// Bad - tracks entities unnecessarily
var users = await _context.Users.ToListAsync();

// Good - much faster for read-only scenarios
var users = await _context.Users.AsNoTracking().ToListAsync();

2. Use Projections Instead of Loading Full Entities

// Bad - loads all columns
var users = await _context.Users.ToListAsync();

// Good - only loads what you need
var userDtos = await _context.Users
    .Select(u => new UserDto
    {
        Id = u.Id,
        Name = u.Name,
        Email = u.Email
    })
    .ToListAsync();

3. Configure Indexes Properly

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BlogPost>()
        .HasIndex(b => b.Slug)
        .IsUnique();

    modelBuilder.Entity<BlogPost>()
        .HasIndex(b => b.PublishedDate);
}

4. Use Split Queries for Complex Includes

var orders = await _context.Orders
    .Include(o => o.Items)
    .Include(o => o.Customer)
    .AsSplitQuery()
    .ToListAsync();

5. Handle Migrations Carefully

  • Always review generated migrations before applying
  • Use dotnet ef migrations script for production deployments
  • Never use Database.Migrate() in production startup

Conclusion

These practices have saved me countless hours of debugging and performance tuning. Apply them consistently, and your EF Core applications will be production-ready from day one.

Share this post

Comments

Ajit Gangurde

Software Engineer II at Microsoft | 15+ years in .NET & Azure