比较 EF Core 和 EF6Compare EF Core & EF6

Entity Framework 是适用于.NET 的对象关系映射程序 (O/RM)。Entity Framework is an object-relational mapper (O/RM) for .NET. 本文比较了两个版本:Entity Framework 6 和 Entity Framework Core。This article compares the two versions: Entity Framework 6 and Entity Framework Core.

Entity Framework 6Entity Framework 6

Entity Framework 6 (EF6) 是一种久经验证的数据访问技术。Entity Framework 6 (EF6) is a tried and tested data access technology. 2008 年,它作为 .NET Framework 3.5 SP1 和 Visual Studio 2008 SP1 的一部分首次发布。It was first released in 2008, as part of .NET Framework 3.5 SP1 and Visual Studio 2008 SP1. 从 4.1 版开始,已作为 EntityFramework NuGet 包发布。Starting with the 4.1 release it has shipped as the EntityFramework NuGet package. 从 3.0 版开始,EF6 在 .NET Framework 4.x 和 .NET Core 上运行。EF6 runs on the .NET Framework 4.x and .NET Core from 3.0 onwards.

EF6 产品仍受支持,并将继续提供 bug 修复和细微改进。EF6 continues to be a supported product, and will continue to see bug fixes and minor improvements.

Entity Framework CoreEntity Framework Core

Entity Framework Core (EF Core) 是在 2016 年首次发布的 EF6 的完全重写。Entity Framework Core (EF Core) is a complete rewrite of EF6 that was first released in 2016. 它附带于 Nuget 包中,是 Microsoft.EntityFrameworkCore 的主要组成部分。It ships in Nuget packages, the main one being Microsoft.EntityFrameworkCore. EF Core 是一款跨平台产品,可以在 .NET Core 上运行。EF Core is a cross-platform product that runs on .NET Core.

EF Core 旨在提供类似于 EF6 的开发人员体验。EF Core was designed to provide a developer experience similar to EF6. 大多数顶级 API 保持不变,因此,用过 EF6 的开发人员都会对 EF Core 感到很熟悉。Most of the top-level APIs remain the same, so EF Core will feel familiar to developers who have used EF6.

功能比较Feature comparison

EF Core 提供了在 EF6 中不会实现的新功能(如备选键批量更新以及 LINQ 查询中的混合客户端/数据库评估EF Core offers new features that won't be implemented in EF6 (such as alternate keys, batch updates, and mixed client/database evaluation in LINQ queries. 但由于它是一个新代码库,所以会缺少一些 EF6 中的功能。But because it's a new code base, it also lacks some features that EF6 has.

下表比较了 EF Core 和 EF6 中可用的功能。The following tables compare the features available in EF Core and EF6. 这只是大致比较,没有列出全部功能或解释不同 EF 版本中相同功能之间的差异。It's 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 EF 6 EF 6 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(部分)1.0 (partial)
数据注释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
继承:每个具体类一个表 (TPC)Inheritance: Table per concrete class (TPC) Yes
阴影状态属性Shadow state properties 1.01.0
备用键Alternate keys 1.01.0
多对多,无联接实体Many-to-many without join entity Yes
密钥生成:数据库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
模型的图形可视化效果Graphical visualization of model Yes
图形模型编辑器Graphical model editor Yes
模型格式:代码Model format: Code Yes 1.01.0
模型格式:EDMX (XML)Model format: EDMX (XML) Yes
从数据库更新模型:命令行Create model from database: Command line Yes 1.01.0
从数据库更新模型:VS 向导Create model from database: VS wizard Yes
从数据库更新模型Update model from database 部分Partial
全局查询筛选器Global query filters 2.02.0
表拆分Table splitting Yes 2.02.0
实体拆分Entity splitting Yes
数据库标量函数映射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

查询数据Querying data

功能Feature EF6 EF6 EF CoreEF Core
LINQ 查询LINQ queries Yes 1.0(进行中,针对复杂查询)1.0 (in-progress for complex queries)
可读内容生成的 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
加载关联数据:ExplicitLoading 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
基于文本的查询语言(实体 SQL)Text-based query language (Entity SQL) Yes
await foreach (C# 8.0)await foreach (C# 8.0) 3.03.0

保存数据Saving data

功能Feature EF6 EF6 EF CoreEF Core
更改跟踪:快照Change tracking: Snapshot Yes 1.01.0
更改跟踪:通知Change tracking: Notification Yes 1.01.0
更改跟踪:代理Change tracking: Proxies Yes
访问跟踪的状态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
断开连接低级别 API 图形Disconnected graph low-level APIs Poor 1.01.0
断开连接端到端图形Disconnected graph End-to-end 1.0(部分)1.0 (partial)

其他功能Other features

功能Feature EF6 EF6 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
生命周期挂钩(事件、截取)Lifecycle hooks (events, interception) Yes
简单的日志记录 (Database.Log)Simple Logging (Database.Log) Yes
DbContext 池DbContext pooling 2.02.0

数据库提供程序Database providers

功能Feature EF6 EF6 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 (1)1.0 (1)
DB2DB2 Yes 1.01.0
FirebirdFirebird Yes 2.02.0
Jet (Microsoft Access)Jet (Microsoft Access) 2.0 (1)2.0 (1)
Cosmos DBCosmos DB 3.03.0
内存中(用于测试)In-memory (for testing) 1.01.0

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

.NET 实现.NET implementations

功能Feature EF6 EF6 EF CoreEF Core
.NET Framework.NET Framework Yes 1.0(在 3.0 中删除)1.0 (removed in 3.0)
.NET Core.NET Core 是(在 6.3 中添加)Yes (added in 6.3) 1.01.0
Mono 和 XamarinMono & Xamarin 1.0(进行中)1.0 (in-progress)
UWPUWP 1.0(进行中)1.0 (in-progress)

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

如果以下两个条件都成立,请考虑为新应用程序使用 EF Core:Consider using EF Core for a new application if both of the following conditions are true:

如果以下两个条件都成立,请考虑使用 EF6:Consider using EF6 if both of the following conditions are true:

  • 应用将在 Windows 和 .NET Framework 4.0 或更高版本上运行。The app will run on Windows and the .NET Framework 4.0 or later.
  • EF6 支持应用需要的所有功能。EF6 supports all of the features that the app requires.

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

由于 EF Core 有一些根本性变化,我们不建议将 EF6 应用程序迁移至 EF Core,除非有令人信服的理由进行此项更改。Because of the fundamental changes in EF Core, we do not recommend moving an EF6 application to EF Core unless there is a compelling reason to make the change. 如果想迁移到 EF Core 以使用新功能,请确保你了解其各项限制。If you want to move to EF Core to use new features, make sure you're aware of its limitations. 有关详细信息,请参阅从 EF6 到 EF Core 的迁移For more information, see Porting from EF6 to EF Core. 从 EF6 到 EF Core 更像是迁移而不是升级。The move from EF6 to EF Core is more a port than an upgrade.

后续步骤Next steps

有关详细信息,请参阅文档:For more information, see the documentation: