efcore-patterns

Configure EF Core with NoTracking defaults, CLI migrations, retry strategies, and bulk updates.

1.1k|101|Updated Nov 12, 2025
One-click install
npx skills add https://github.com/Aaronontheweb/dotnet-skills --skill efcore-patterns-aaronontheweb
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: efcore-patterns
Source: https://github.com/Aaronontheweb/dotnet-skills/tree/main/skills/efcore-patterns
Command: npx skills add https://github.com/Aaronontheweb/dotnet-skills --skill efcore-patterns-aaronontheweb

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill codifies best practices for EF Core usage to maximize performance, reliability, and maintainability across data access layers. It addresses common pitfalls such as unnecessary tracking, manual migration edits, and inconsistent transaction handling, providing a structured approach to configure DbContext, migrations, and data access patterns.

Core Features & Use Cases

  • NoTracking by default for read-only queries to improve performance; explicit tracking or Update when needed.
  • Never edit migrations manually; rely on EF Core CLI for creating, applying, and scripting migrations.
  • Dedicated migration service with Aspire to run migrations before app startup and clean seeding behavior.
  • Execution strategies for transient failures with retry policies and safe transaction usage inside strategy callbacks.
  • Bulk operations using ExecuteUpdate/ExecuteDelete to perform set-based updates and deletes without loading entities.
  • Global query splitting to prevent cartesian explosion when loading multiple navigation collections; per-query override possible.
  • Guidance on DbContext lifetime (scoped per request in ASP.NET Core, or per operation in background/actors with scope or factory).
  • Testing recommendations with EF Core; in-memory provider for unit tests; testcontainers for integration tests.

Quick Start

To implement EF Core patterns in your app, configure your DbContext with NoTracking default, enable query splitting globally, set up a dedicated migration service for Aspire, and adopt bulk operations for large updates. See example usage in the Skill notes for concrete code changes and commands.

Frequently Asked Questions about efcore-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I improve EF Core query performance for read-heavy APIs?

EF Core query performance improves by using NoTracking by default for read-only queries, enabling global query splitting to prevent cartesian explosion, and applying set-based bulk updates via ExecuteUpdate and ExecuteDelete without loading entities.

What's the best way to run EF Core migrations in an Aspire-enabled service?

Run EF Core migrations in Aspire using a dedicated migration service that executes before app startup, relying on EF Core CLI to create and apply migrations without manual edits and ensuring clean database seeding behavior.

How do I handle transient failures and transactions in EF Core?

Handle transient failures in EF Core by configuring execution strategies with retry policies, and ensure safe transaction usage by executing transaction logic inside the execution strategy callbacks to maintain data integrity during retries.

Can I use ExecuteUpdate and ExecuteDelete for bulk operations without loading entities?

Yes, you can perform bulk updates and deletes in EF Core using ExecuteUpdate and ExecuteDelete to execute set-based operations directly against the database without loading entities into memory first.

How should I manage DbContext lifetime in background jobs and microservices?

Manage EF Core DbContext lifetime by scoping it per request in ASP.NET Core, and using per-operation scopes or a DbContext factory for background jobs and microservices to prevent concurrency issues.

What testing strategies work best with EF Core data access layers?

Test EF Core data access layers using the in-memory provider for unit tests and testcontainers for integration tests to validate migrations, query splitting, and retry execution strategies against real database behavior.