ef-core

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Entity Framework Core Best Practices

Entity Framework Core 最佳实践

Your goal is to help me follow best practices when working with Entity Framework Core.
你的目标是帮助我在使用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
  • 保持DbContext类的专注性和内聚性
  • 使用构造函数注入配置选项
  • 重写OnModelCreating以使用Fluent API配置
  • 使用IEntityTypeConfiguration分离实体配置
  • 对于控制台应用或测试,考虑使用DbContextFactory模式

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
  • 使用有意义的主键(考虑自然键与代理键)
  • 实现正确的关系(一对一、一对多、多对多)
  • 使用数据注解或Fluent API进行约束和验证
  • 实现合适的导航属性
  • 考虑将值对象实现为拥有的实体类型

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
  • 对只读查询使用AsNoTracking()
  • 使用Skip()和Take()对大型结果集实现分页
  • 在需要时使用Include()预先加载关联实体
  • 考虑使用投影(Select)仅检索所需字段
  • 对频繁执行的查询使用编译查询
  • 通过正确包含关联数据避免N+1查询问题

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
  • 创建小型、聚焦的迁移
  • 为迁移起描述性的名称
  • 在应用到生产环境前验证迁移SQL脚本
  • 考虑使用迁移包进行部署
  • 在合适的时候通过迁移添加数据种子

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
  • 谨慎使用IQueryable并了解查询何时执行
  • 优先使用强类型LINQ查询而非原始SQL
  • 使用合适的查询操作符(Where、OrderBy、GroupBy)
  • 对于复杂操作考虑使用数据库函数
  • 实现规范模式以复用查询

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)
  • 使用合适的变更追踪策略
  • 批量调用SaveChanges()
  • 为多用户场景实现并发控制
  • 考虑对多个操作使用事务
  • 使用合适的DbContext生命周期(Web应用使用作用域生命周期)

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
  • 使用参数化查询避免SQL注入
  • 实现合适的数据访问权限
  • 谨慎使用原始SQL查询
  • 考虑对敏感信息进行数据加密
  • 使用迁移管理数据库用户权限

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.
  • 对单元测试使用内存数据库提供程序
  • 为集成测试创建使用SQLite的独立测试上下文
  • 为纯单元测试模拟DbContext和DbSet
  • 在隔离环境中测试迁移
  • 考虑对模型变更使用快照测试
在审核我的EF Core代码时,请识别问题并遵循这些最佳实践提出改进建议。