比较 EF Core 和 EF6Compare EF Core & EF6

EF CoreEF Core

Entity Framework Core (EF Core) 是适用于 .NET 的新式对象数据库映射器。Entity Framework Core (EF Core) is a modern object-database mapper for .NET. 它支持 LINQ 查询、更改跟踪、更新和架构迁移。It supports LINQ queries, change tracking, updates, and schema migrations.

EF Core 通过数据库提供程序插件模型与 SQL Server/SQL Azure、SQLite、Azure Cosmos DB、MySQL、PostgreSQL 和更多数据库配合使用。EF Core works with SQL Server/SQL Azure, SQLite, Azure Cosmos DB, MySQL, PostgreSQL, and many more databases through a database provider plugin model.

EF6EF6

Entity Framework 6 (EF6) 是专为 .NET Framework 设计的对象关系映射器,但支持 .NET Core。Entity Framework 6 (EF6) is an object-relational mapper designed for .NET Framework but with support for .NET Core. EF6 是一款受支持的稳定产品,但我们不再对其进行积极开发。EF6 is a stable, supported product, but is no longer being actively developed.

功能比较Feature comparison

EF Core 提供了不会在 EF6 中实现的新功能。EF Core offers new features that won't be implemented in EF6. 但是,并非所有 EF6 功能都已在 EF Core 中实现。However, not all EF6 features are currently implemented in EF Core.

下表比较了 EF Core 和 EF6 中可用的功能。The following tables compare the features available in EF Core and EF6. 这只是大致比较,没有列出全部功能,也未解释不同 EF 版本中相同功能之间的差异。This is a high-level comparison and doesn't list every feature or explain differences between the same feature in different EF versions.

EF Core 列指出了功能首次出现的产品版本。The EF Core column indicates the product version in which the feature first appeared.

创建模型Creating a model

功能Feature EF6.4EF6.4 EF CoreEF Core
基本类映射Basic class mapping Yes 1.01.0
带有参数的构造函数Constructors with parameters 2.12.1
属性值转换Property value conversions 2.12.1
没有键的映射类型Mapped types with no keys 2.12.1
约定Conventions Yes 1.01.0
自定义约定Custom conventions Yes 1.0(部分;#2141.0 (partial; #214)
数据注释Data annotations Yes 1.01.0
Fluent APIFluent API Yes 1.01.0
继承:每个层次结构一张表 (TPH)Inheritance: Table per hierarchy (TPH) Yes 1.01.0
继承:每个类型一张表 (TPT)Inheritance: Table per type (TPT) Yes 计划在 5.0 版中推出 (#2266)Planned for 5.0 (#2266)
继承:每个具体类一张表 (TPC)Inheritance: Table per concrete class (TPC) Yes 5.0 版的延伸目标 (#3170) (1)Stretch for 5.0 (#3170) (1)
阴影状态属性Shadow state properties 1.01.0
备用键Alternate keys 1.01.0
多对多导航Many-to-many navigations Yes 计划在 5.0 版中推出 (#19003)Planned for 5.0 (#19003)
多对多,无联接实体Many-to-many without join entity Yes 积压工作 (#1368)On the backlog (#1368)
密钥生成:数据库Key generation: Database Yes 1.01.0
密钥生成:客户端Key generation: Client 1.01.0
复杂/已拥有类型Complex/owned types Yes 2.02.0
空间数据Spatial data Yes 2.22.2
模型格式:代码Model format: Code Yes 1.01.0
从数据库创建模型:命令行Create model from database: Command line Yes 1.01.0
从数据库更新模型Update model from database 部分Partial 积压工作 (#831)On the backlog (#831)
全局查询筛选器Global query filters 2.02.0
表拆分Table splitting Yes 2.02.0
实体拆分Entity splitting Yes 5.0 版的延伸目标 (#620) (1)Stretch for 5.0 (#620) (1)
数据库标量函数映射Database scalar function mapping Poor 2.02.0
字段映射Field mapping 1.11.1
可为空引用类型 (C# 8.0)Nullable reference types (C# 8.0) 3.03.0
模型的图形可视化效果Graphical visualization of model Yes 未计划支持 (2)No support planned (2)
图形模型编辑器Graphical model editor Yes 未计划支持 (2)No support planned (2)
模型格式:EDMX (XML)Model format: EDMX (XML) Yes 未计划支持 (2)No support planned (2)
从数据库创建模型:VS 向导Create model from database: VS wizard Yes 未计划支持 (2)No support planned (2)

正在查询数据Querying data

功能Feature EF6.4EF6.4 EF CoreEF Core
LINQ 查询LINQ queries Yes 1.01.0
可读内容生成的 SQLReadable generated SQL Poor 1.01.0
GroupBy 转换GroupBy translation Yes 2.12.1
加载相关数据:预先加载Loading related data: Eager Yes 1.01.0
加载相关数据:预先加载派生类型Loading related data: Eager loading for derived types 2.12.1
加载相关数据:延迟加载Loading related data: Lazy Yes 2.12.1
加载相关数据:显式加载Loading related data: Explicit Yes 1.11.1
原始 SQL 查询:实体类型Raw SQL queries: Entity types Yes 1.01.0
原生 SQL 查询:无键实体类型Raw SQL queries: Keyless entity types Yes 2.12.1
原始 SQL 查询:使用 LINQ 编写Raw SQL queries: Composing with LINQ 1.01.0
显式编译的查询Explicitly compiled queries Poor 2.02.0
await foreach (C# 8.0)await foreach (C# 8.0) 3.03.0
基于文本的查询语言(实体 SQL)Text-based query language (Entity SQL) Yes 未计划支持 (2)No support planned (2)

保存数据Saving data

功能Feature EF6.4EF6.4 EF CoreEF Core
更改跟踪:快照Change tracking: Snapshot Yes 1.01.0
更改追踪:通知Change tracking: Notification Yes 1.01.0
更改跟踪:代理Change tracking: Proxies Yes 已在 5.0 版中合并 (#10949)Merged for 5.0 (#10949)
访问跟踪的状态Accessing tracked state Yes 1.01.0
开放式并发Optimistic concurrency Yes 1.01.0
事务Transactions Yes 1.01.0
批处理语句Batching of statements 1.01.0
存储过程映射Stored procedure mapping Yes 积压工作 (#245)On the backlog (#245)
断开连接低级别 API 图形Disconnected graph low-level APIs Poor 1.01.0
断开连接端到端图形Disconnected graph End-to-end 1.0(部分;#55361.0 (partial; #5536)

其他功能Other features

功能Feature EF6.4EF6.4 EF CoreEF Core
迁移Migrations Yes 1.01.0
数据库创建/删除 APIDatabase creation/deletion APIs Yes 1.01.0
种子数据Seed data Yes 2.12.1
连接复原Connection resiliency Yes 1.11.1
拦截器Interceptors Yes 3.03.0
事件Events Yes 3.0(部分;#6263.0 (partial; #626)
简单的日志记录 (Database.Log)Simple Logging (Database.Log) Yes 已在 5.0 版中合并 (#1199)Merged for 5.0 (#1199)
DbContext 池DbContext pooling 2.02.0

数据库提供程序(3)Database providers (3)

功能Feature EF6.4EF6.4 EF CoreEF Core
SQL ServerSQL Server Yes 1.01.0
MySQLMySQL Yes 1.01.0
PostgreSQLPostgreSQL Yes 1.01.0
OracleOracle Yes 1.01.0
SQLiteSQLite Yes 1.01.0
SQL Server CompactSQL Server Compact Yes 1.0 (4)1.0 (4)
DB2DB2 Yes 1.01.0
FirebirdFirebird Yes 2.02.0
Jet (Microsoft Access)Jet (Microsoft Access) 2.0 (4)2.0 (4)
Azure Cosmos DBAzure Cosmos DB 3.03.0
内存中(用于测试)In-memory (for testing) 1.01.0

1 在给定版本中,不太可能实现延伸目标。1 Stretch goals are not likely to be achieved for a given release. 但如果一切顺利,我们将尝试加入相关功能。However, if things go well, then we will try to pull them in.

2 EF Core 中不会实现某些 EF6 功能。2 Some EF6 features will not be implemented in EF Core. 这些功能依赖于 EF6 的基础实体数据模型 (EDM),并且/或者是复杂功能,投资回报率相对较低。These features either depend on EF6's underlying Entity Data Model (EDM) and/or are complex features with relatively low return on investment. 欢迎提出反馈,但是,尽管 EF Core 支持许多在 EF6 中无法实现的功能,反过来,EF Core 支持 EF6 的所有功能却并不可行。We always welcome feedback, but while EF Core enables many things not possible in EF6, it is conversely not feasible for EF Core to support all the features of EF6.

3 更新到新的 EF Core 主版本时,第三方实现的 EF Core 数据库提供程序可能延迟。3 EF Core database providers implemented by third-parties may be delayed in updating to new major versions of EF Core. 有关详细信息,请参阅数据库提供程序See Database Providers for more information.

4 SQL Server Compact 和 Jet 提供程序仅适用于 .NET Framework(而不适用于 .NET Core)。4 The SQL Server Compact and Jet providers only work on .NET Framework (not on .NET Core).

受支持的平台Supported platforms

EF Core 3.1 通过使用 .NET Standard 2.0 在 .NET Core 和 .NET Framework 上运行。EF Core 3.1 runs on .NET Core and .NET Framework, through the use of .NET Standard 2.0. 但 EF Core 5.0 不会在 .NET Framework 上运行。However, EF Core 5.0 will not run on .NET Framework. 有关更多详细信息,请参阅平台See Platforms for more details.

EF6.4 通过多目标在 .NET Core 和 .NET Framework 上运行。EF6.4 runs on .NET Core and .NET Framework, through multi-targeting.

针对新应用程序的选择指南Guidance for new applications

除非应用需要仅在 .NET Framework 上受支持的内容,否则对于所有新应用程序都在 .NET Core 上使用 EF Core。Use EF Core on .NET Core for all new applications unless the app needs something that is only supported on .NET Framework.

针对现有 EF6 应用程序的选择指南Guidance for existing EF6 applications

EF Core 不是 EF6 的直接替换项。EF Core is not a drop-in replacement for EF6. 从 EF6 迁移到 EF Core 可能需要更改应用程序。Moving from EF6 to EF Core will likely require changes to your application.

将 EF6 应用迁移到 .NET Core 时:When moving an EF6 app to .NET Core:

  • 如果数据访问代码稳定且不太可能开发或需要新功能,请继续使用 EF6。Keep using EF6 if the data access code is stable and not likely to evolve or need new features.
  • 如果数据访问代码不断演变,或应用需要仅在 EF Core 中提供的新功能,请迁移到 EF Core。Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core.
  • 迁移到 EF Core 通常也是为了提高性能。Porting to EF Core is also often done for performance. 但是,并非所有方案都可提高性能,因此请先进行分析。However, not all scenarios are faster, so do some profiling first.

有关详细信息,请参阅从 EF6 到 EF Core 的迁移See Porting from EF6 to EF Core for more information.