Generated banner for Ef Core
Generated logo for Ef Core

Ef Core

GitHubOfficialOriginally contributed by @aaronpowell as prompts/ef-core.prompt.md; moved into a skill by pull request #761Distributed by GitHub

  • Skill
  • Guidelines
  • Software development
  • MIT
  • No issues found

From the author

Get best practices for Entity Framework Core
— the description by aaronpowell

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a structured set of best practices for Entity Framework Core, covering essential areas such as data context design, entity modeling, performance optimization, migration management, and security. It serves as a reference for developers to ensure their data access layer is efficient, maintainable, and secure.

What it does

  • Provides architectural guidance for DbContext design and configuration.
  • Offers strategies for efficient entity modeling and relationship management.
  • Outlines performance tuning techniques like projection and tracking control.
  • Details best practices for managing database migrations and deployment.
  • Suggests security measures to prevent SQL injection and manage permissions.
  • Recommends testing strategies including mocking and integration testing approaches.

When to use it

  • When designing a new data access layer using EF Core.
  • When refactoring existing database code for better performance.
  • When preparing for production deployment of database migrations.
  • When debugging performance issues related to query execution.
  • When writing unit or integration tests for database-dependent code.

Advantages

  • Promotes consistent and maintainable data access patterns.
  • Helps identify and prevent common performance pitfalls like N+1 queries.
  • Encourages secure coding practices to protect against SQL injection.
  • Provides clear guidance on testing strategies for database operations.

Usage examples

“Review my current DbContext setup and suggest improvements.”
The agent will analyze your DbContext structure against the provided best practices, suggesting improvements like using IEntityTypeConfiguration or DbContextFactory.
“How can I optimize my LINQ queries for better performance?”
The agent will recommend techniques such as using AsNoTracking, implementing pagination, or using projections to reduce data retrieval overhead.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/github/ef-core/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip ef-core.zip -d .agents/skills

Documentation by aaronpowell

Entity Framework Core Best Practices

Your goal is to help me follow best practices when working with Entity Framework Core.

Data Context Design

  • Keep DbContext classes focused and cohesive

  • Use constructor injection for configuration options

  • Override OnModelCreating for fluent API configuration

  • Separate entity configurations using IEntityTypeConfiguration

  • Consider using DbContextFactory pattern for console apps or tests

Entity Design

  • Use meaningful primary keys (consider natural vs surrogate keys)

  • Implement proper relationships (one-to-one, one-to-many, many-to-many)

  • Use data annotations or fluent API for constraints and validations

  • Implement appropriate navigational properties

  • Consider using owned entity types for value objects

Performance

  • Use AsNoTracking() for read-only queries

  • Implement pagination for large result sets with Skip() and Take()

  • Use Include() to eager load related entities when needed

  • Consider projection (Select) to retrieve only required fields

  • Use compiled queries for frequently executed queries

  • Avoid N+1 query problems by properly including related data

Migrations

  • Create small, focused migrations

  • Name migrations descriptively

  • Verify migration SQL scripts before applying to production

  • Consider using migration bundles for deployment

  • Add data seeding through migrations when appropriate

Querying

  • Use IQueryable judiciously and understand when queries execute

  • Prefer strongly-typed LINQ queries over raw SQL

  • Use appropriate query operators (Where, OrderBy, GroupBy)

  • Consider database functions for complex operations

  • Implement specifications pattern for reusable queries

Change Tracking & Saving

  • Use appropriate change tracking strategies

  • Batch your SaveChanges() calls

  • Implement concurrency control for multi-user scenarios

  • Consider using transactions for multiple operations

  • Use appropriate DbContext lifetimes (scoped for web apps)

Security

  • Avoid SQL injection by using parameterized queries

  • Implement appropriate data access permissions

  • Be careful with raw SQL queries

  • Consider data encryption for sensitive information

  • Use migrations to manage database user permissions

Testing

  • Use in-memory database provider for unit tests

  • Create separate testing contexts with SQLite for integration tests

  • Mock DbContext and DbSet for pure unit tests

  • Test migrations in isolated environments

  • Consider snapshot testing for model changes

When reviewing my EF Core code, identify issues and suggest improvements that follow these best practices.

Files

Requirements and compatibility

Written for: GitHub Copilot

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Originally contributed by
GitHub avatar of aaronpowell@aaronpowellas prompts/ef-core.prompt.mdmoved into a skill by pull request #761
Publisher
GitHub (Official)
Distributed by
GitHub
Source
github/awesome-copilot · skills/ef-core · commit 797337f
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Contributors
aaronpowell, CypherHK, derekclair, jamesmontemagno

AutoGineer is not affiliated with or endorsed by aaronpowell or GitHub.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download ef-core.zip