
Entity Framework Core Best Practices
OfficialFreeFollow best practices for Entity Framework Core development.
Free · Opens the source repo
What Entity Framework Core Best Practices does
Entity Framework Core Best Practices is designed to guide developers in implementing effective strategies while working with Entity Framework Core. This skill provides a comprehensive overview of best practices across various aspects of EF Core, including data context design, entity design, performance optimization, migrations, querying, change tracking, security, and testing. By following these guidelines, developers can enhance the maintainability, performance, and security of their applications.
The skill emphasizes the importance of a well-structured DbContext, advocating for focused and cohesive classes, constructor injection for configuration, and the use of fluent API for model configuration. It also covers entity design principles, such as meaningful primary keys and proper relationship implementation, ensuring that developers create robust data models. Performance tips, like using AsNoTracking() for read-only queries and implementing pagination, help optimize database interactions.
Migrations are addressed with a focus on creating small, descriptive migrations and verifying SQL scripts before production deployment. The skill further explores querying techniques, encouraging the use of IQueryable and strongly-typed LINQ queries. Change tracking and saving strategies are discussed, highlighting the importance of batching SaveChanges() calls and implementing concurrency control.
Security best practices are also included, such as avoiding SQL injection and managing database user permissions. Finally, the skill provides guidance on testing, recommending the use of in-memory databases for unit tests and mocking DbContext for pure unit tests. Overall, this skill is an essential resource for developers seeking to improve their EF Core applications by adhering to industry best practices.
When to use it
Use this skill when developing applications with Entity Framework Core to ensure best practices are followed throughout the development process.
When not to use it
This skill may not be suitable for users unfamiliar with Entity Framework Core or those seeking advanced, niche optimizations.
What you can build with it
Optimizing Query Performance
When facing performance issues with database queries, this skill offers techniques like using AsNoTracking() and pagination to enhance efficiency.
Managing Migrations Effectively
If you're struggling with migration management, this skill provides guidance on creating small, focused migrations and verifying SQL scripts before deployment.
Ensuring Application Security
Use this skill to implement security best practices in your EF Core applications, such as avoiding SQL injection and managing database permissions.
How to install Entity Framework Core Best Practices
View source1. Install with the skills CLI
npx skills add github/awesome-copilot/ef-core --agent claude-code2. Or install it manually
Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.
Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs
Inside SKILL.md
Written by githubEntity 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.
Frequently asked questions about Entity Framework Core Best Practices
Similar skills
ClickHouse Logs Queries
Efficiently manage Supabase logs with ClickHouse SQL.
EF Core D2 Database Diagram Generator
Visualize your EF Core models as D2 diagrams effortlessly.
Safe SQL Execution
Ensure secure SQL execution in Supabase applications.
Oracle to PostgreSQL Migration
Identify migration risks between Oracle and PostgreSQL.
SSMA Console
Streamline Oracle to SQL Server migrations with ease.
SQL Performance Optimization
Enhance SQL query efficiency across all databases.
