- Home
- Skills
- Aaronontheweb
- Dotnet Skills
- Efcore Patterns
efcore-patterns_skill
- Shell
643
GitHub Stars
1
Bundled Files
2 months ago
Catalog Refreshed
4 months ago
First Indexed
Readme & install
Copy the install command, review bundled files from the catalogue, and read any extended description pulled from the listing source.
Installation
Preview and clipboard use veilstrat where the catalogue uses aiagentskills.
npx veilstrat add skill aaronontheweb/dotnet-skills --skill efcore-patterns- SKILL.md17.7 KB
Overview
This skill codifies Entity Framework Core best practices for .NET projects, focusing on read performance, safe migrations, robust retry strategies, and common pitfalls to avoid. It provides concrete patterns like NoTracking by default, query splitting for navigation collections, dedicated migration services, and bulk operations with ExecuteUpdate/ExecuteDelete.
How this skill works
The skill teaches configuration and code patterns to improve EF Core reliability and performance: set DbContext to NoTracking for read-heavy workloads, explicitly mark entities for updates, use execution strategies for transient failures, and run migrations via a dedicated migration service. It also prescribes CLI usage for creating, removing, and applying migrations and recommends bulk SQL operations instead of loading entities into memory.
When to use it
- Setting up EF Core in a new application
- Optimizing read-heavy queries and reducing change-tracking overhead
- Managing and applying database migrations safely in CI/CD
- Running migrations separately from the main app (migration service)
- Performing bulk updates or deletes efficiently
- Handling transient database failures with retry strategies
Best practices
- Configure ChangeTracker.QueryTrackingBehavior = NoTracking and opt into tracking with AsTracking() when needed
- Never edit migration files manually; use dotnet ef CLI commands for add/remove/update and script generation
- Run migrations from a dedicated MigrationService that applies pending migrations and stops when complete
- Wrap retryable DB work inside Database.CreateExecutionStrategy().ExecuteAsync and keep transactions inside the callback
- Use ExecuteUpdateAsync/ExecuteDeleteAsync for large bulk operations instead of loading entities
- Keep DbContext scoped per request and avoid cross-context tracking conflicts (detach or use single context)
Example use cases
- API GET endpoints and read-only pages that must be fast and scalable using NoTracking
- A CI job or container that runs a MigrationService to apply migrations before the API starts
- Updating order status with AsTracking or explicit Update() when DbContext is NoTracking by default
- Retrying a payment update operation with CreateExecutionStrategy and an in-callback transaction
- Cleaning up expired records nightly using ExecuteDeleteAsync to run a single SQL DELETE
FAQ
If DbContext is configured NoTracking by default, modifying an entity retrieved without AsTracking() will not be saved unless you call Update() or attach the entity explicitly.
Can I edit migration files if I need custom SQL?
You should not edit migration scaffolding except to add custom SQL inside the Up()/Down() methods; avoid renaming, deleting, or copying migration files manually.