ef-core-patterns

michaellperry's avatarfrom michaellperry

Entity Framework Core best practices for configuration, queries, concurrency, and multi-tenancy.

0stars🔀0forks📁View on GitHub🕐Updated Jan 11, 2026

When & Why to Use This Skill

This Claude skill provides a comprehensive guide to Entity Framework Core (EF Core) best practices, focusing on optimized entity configuration, high-performance query patterns, robust concurrency management, and scalable multi-tenant data isolation for .NET developers.

Use Cases

  • Optimizing database performance: Implementing AsNoTracking, projections, and split queries to prevent N+1 problems and reduce memory overhead.
  • Implementing Multi-tenancy: Using global query filters and indexed tenant columns to ensure secure data isolation across multiple clients.
  • Handling Concurrency: Configuring RowVersion tokens and implementing conflict resolution logic to maintain data integrity in high-concurrency environments.
  • Clean Architecture: Organizing database logic using Fluent API configurations and the Repository pattern to keep domain entities decoupled from infrastructure.
nameef-core-patterns
descriptionEntity Framework Core best practices for configuration, queries, concurrency, and multi-tenancy.

Entity Framework Core Best Practices

Use when configuring entities, optimizing queries, or implementing multi-tenant data access with EF Core.

When to use

  • Configuring entity mappings, indexes, relationships, and value objects
  • Optimizing read queries (AsNoTracking, projections) or preventing N+1 problems
  • Adding row version concurrency tokens or handling conflicts
  • Applying global tenant filters or implementing repositories

Core principles

  • Prefer Fluent API over data annotations; keep entities clean with private setters
  • Use AsNoTracking for reads; Include/split queries to avoid N+1; project to DTOs when possible
  • Index tenant columns and frequent filter combinations; create unique composites for business rules
  • Add RowVersion for optimistic concurrency; catch DbUpdateConcurrencyException
  • Apply global query filters for multi-tenancy; bypass with IgnoreQueryFilters when needed
  • Encapsulate in repositories or inject DbContext directly into services

Resources

Default locations

  • Entity configurations: src/GloboTicket.Infrastructure/Persistence/Configurations
  • DbContext: src/GloboTicket.Infrastructure/Persistence/GloboTicketDbContext.cs
  • Repositories (if used): src/GloboTicket.Infrastructure/Persistence/Repositories
  • Migrations: src/GloboTicket.Infrastructure/Migrations

Validation checklist

  • Entities have private setters and parameterless constructors for EF
  • Configurations use Fluent API with separate IEntityTypeConfiguration classes
  • Tenant columns indexed; composite indexes match query patterns
  • Read-only queries use AsNoTracking and project to DTOs
  • Include/split queries prevent N+1; large collections use AsSplitQuery
  • Concurrency tokens configured where updates are common
  • Global filters applied to ITenantEntity types; IgnoreQueryFilters used sparingly
  • SaveChanges called in repositories or services consistently