dotnet-efcore-patterns

Manage EF Core DbContext lifecycle and optimize query performance in .NET applications.

Updated Aug 16, 2025
One-click install
npx skills add https://github.com/dodyg/blue-nile-pds --skill dotnet-efcore-patterns-dodyg
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-efcore-patterns
Source: https://github.com/dodyg/blue-nile-pds/tree/main/.agents/skills/dotnet-efcore-patterns
Command: npx skills add https://github.com/dodyg/blue-nile-pds --skill dotnet-efcore-patterns-dodyg

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This guide solves common EF Core pitfalls that cause memory bloat, incorrect DbContext usage, inefficient queries, and unsafe migration workflows by prescribing lifetime, tracking, and resiliency patterns for reliable data access in .NET applications.

Core Features & Use Cases

  • Scoped DbContext lifecycle guidance and IDbContextFactory usage for background services and Blazor Server to avoid captured or long-lived contexts.
  • Read optimization with AsNoTracking and context-level QueryTrackingBehavior to reduce memory and CPU for read-heavy services.
  • Query splitting recommendations to prevent Cartesian explosion when including multiple collection navigations and tradeoffs between atomicity and round-trips.
  • Migration workflows and migration bundle instructions for safe production deployments along with best practices around idempotent scripts and avoiding Database.Migrate at startup.
  • Interceptors, compiled queries, and connection resiliency patterns for automatic auditing, soft deletes, high-frequency query optimization, and transient failure retries.
  • Use case: converting a synchronous order-report endpoint that loads orders with multiple child collections into an efficient, read-only, split-query flow registered on a scoped DbContext.

Quick Start

Show me how to register a scoped AppDbContext, enable AsNoTracking defaults for read-only services, and convert an Include-heavy query to use AsSplitQuery to avoid Cartesian explosion.

Frequently Asked Questions about dotnet-efcore-patterns

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

FAQPage Schema
How do I prevent Cartesian explosion in EF Core queries with multiple Include statements?

To prevent Cartesian explosion in EF Core, apply the AsSplitQuery pattern to load multiple collection navigations across separate round-trips instead of one massive Cartesian product, trading query atomicity for reduced memory bloat.

What is the best way to manage DbContext lifetime in Blazor Server and background services?

The best way to manage DbContext lifetime in Blazor Server and background services is using IDbContextFactory to create short-lived contexts, avoiding captured or long-lived scoped contexts that cause memory leaks and concurrency issues.

How do I enable AsNoTracking globally for read-heavy EF Core APIs?

To enable AsNoTracking globally for read-heavy EF Core APIs, set context-level QueryTrackingBehavior.NoTracking on your scoped DbContext registration, reducing memory and CPU overhead by skipping unnecessary change tracking.

Does EF Core support safe production database migrations without application startup?

Yes, EF Core supports safe production migrations by generating migration bundles or idempotent scripts for deployment pipelines, avoiding unsafe Database.Migrate calls at application startup.

How do I implement connection resiliency and retry logic for EF Core?

To implement connection resiliency and retry logic for EF Core, configure retry execution strategies in your DbContext setup to automatically handle transient database failures and maintain reliable data access.

Can I use EF Core compiled queries for high-frequency endpoint optimization?

Yes, you can use EF Core compiled queries for high-frequency endpoint optimization by pre-compiling LINQ queries into delegates, reducing query translation overhead and improving response times for repeated database access.