ef-core

Guide EF Core DbContextFactory usage, query optimization, and migration management.

Updated Dec 23, 2025
One-click install
npx skills add https://github.com/yaochangyu/ai-dev-toolkit --skill ef-core-yaochangyu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ef-core
Source: https://github.com/yaochangyu/ai-dev-toolkit/tree/main/.github/skills/ef-core
Command: npx skills add https://github.com/yaochangyu/ai-dev-toolkit --skill ef-core-yaochangyu

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) and assets (resource) components.

What problem does it solve?

EF Core usage often suffers from incorrect DbContext lifetimes, unoptimized queries, and migration management challenges. This Skill consolidates best practices into clear guidance, patterns, and templates to improve reliability and performance.

Core Features & Use Cases

  • DbContextFactory usage guidance to ensure proper lifetimes in dependency injection.
  • Query optimization techniques including AsNoTracking, Include/ThenInclude, projection, and paging.
  • Migration management and reverse engineering workflows to evolve databases safely and predictably.
  • Template-driven codegen and scaffolding to accelerate EF Core projects.

Quick Start

Configure your DI container to use a DbContextFactory, enable AsNoTracking for read-only queries, apply Include/ThenInclude for related data, and use dotnet ef commands to add and apply migrations, then scaffold reverse-engineered DbContext/models as needed.

Frequently Asked Questions about ef-core

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

FAQPage Schema
How do I optimize EF Core queries to avoid performance issues?

EF Core query optimization uses AsNoTracking for read-only data, Include and ThenInclude for eager loading related entities, and projection to select only needed fields. Apply paging to limit result sets and reduce memory overhead.

What is the best way to manage DbContext lifetime in dependency injection?

DbContextFactory provides proper DbContext lifetime management in dependency injection scenarios. Use factory patterns to create disposable context instances, ensuring safe database access and preventing concurrency conflicts in EF Core applications.

How do I reverse engineer an existing database with EF Core?

EF Core reverse engineering workflows scaffold DbContext and entity models from an existing database. Use database-first code generation commands to generate code templates, accelerating project setup while maintaining safe and predictable schema evolution.

Why should I use AsNoTracking in my EF Core read queries?

AsNoTracking skips change tracking in EF Core read-optimized queries, reducing memory allocation and improving performance. Apply it to read-only operations where entity state monitoring is unnecessary, ensuring efficient data retrieval without tracking overhead.

How do I apply migrations safely in an EF Core project?

EF Core migration management uses dotnet ef commands to add and apply database schema changes predictably. Follow template-driven workflows to evolve databases safely, ensuring migrations are applied sequentially and reverse engineering workflows remain stable.

Can I use DbContextFactory with dependency injection for multi-tenant databases?

DbContextFactory supports dependency injection configurations for managing multiple database contexts. It enforces proper factory lifecycle patterns, enabling safe DbContext instantiation for varied tenant scenarios while maintaining query optimization and migration integrity.