ef-core
它能做什么
这个助手可以帮你编写更好、更高效的 Entity Framework Core 代码。Entity Framework Core 是 C# 中用于操作数据库的工具。
适用场景
- ✓创建或修改使用 Entity Framework Core 的代码时。
- ✓希望确保代码运行快速、安全时。
- ✓遇到性能问题或数据库错误时。
技能链接
https://github.com/github/awesome-copilot/blob/main/skills/ef-core/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“怎样优化我的查询,避免数据库运行缓慢?”
每周 5 个新技能,1 分钟读完。
工作原理
它会将你的代码与一套规则和最佳实践进行比对。如果发现有可以改进的地方,就会建议你如何修改。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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.
常见问题
什么是 Entity Framework Core?
它是一个工具,能让你用 C# 编写的程序更轻松地与数据库通信,而不需要编写复杂的 SQL 命令。
什么是“最佳实践”?
它们是人们公认的最好做法,通常能让代码运行更快、更安全,也更容易维护。
使用这个助手需要很懂数据库吗?
不需要。即使你是 Entity Framework Core 的新手,这个助手也能提供帮助。
详情
热度
- GitHub 星标
- 40k
- 排名
- #7186
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
acquire-codebase-knowledge
40k帮你的智能体理解并编写现有代码项目的文档。它有助于新成员快速上手,或者快速了解项目架构。
acreadiness-assess
40k让你的智能体评估一个项目对 AI 的准备程度,并生成报告。它有助于了解项目在使用 AI 工具方面的准备情况。
acreadiness-generate-instructions
40k帮你的智能体根据评估结果编写 AI 工具的指令文件。它有助于改善 AI 与代码的交互方式,并弥补已发现的不足。
acreadiness-policy
40k让你的智能体协助制定或应用 AI 项目评估的政策。它有助于规范项目的评估方式,并确保合规。