跳到正文
Skillpedia
通用编程40k · #7186

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/ef-core/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 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 的新手,这个助手也能提供帮助。

详情

作者的原始描述

Get best practices for Entity Framework Core

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7186
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能