比較 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) 是完全重寫的 EF6,首次發行時間為 2016 年。Entity Framework Core (EF Core) is a complete rewrite of EF6 that was first released in 2016. 它隨附於 Nuget 套件,主要項目為 Microsoft.EntityFrameworkCoreIt 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 6EF 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 PartialPartial
全域查詢篩選條件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
可為 Null 的參考型別 (C# 8.0)Nullable reference types (C# 8.0) 3.03.0

查詢資料Querying data

功能Feature EF6EF6 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
載入相關資料:明確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
以文字為基礎的查詢語言 (Entity SQL)Text-based query language (Entity SQL) Yes
await foreach (C# 8.0)await foreach (C# 8.0) 3.03.0

儲存資料Saving data

功能Feature EF6EF6 EF CoreEF Core
變更追蹤:快照集Change tracking: Snapshot Yes 1.01.0
變更追蹤:通知Change tracking: Notification Yes 1.01.0
變更追蹤:ProxyChange 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
已中斷連線的圖形低階 APIDisconnected graph low-level APIs Poor 1.01.0
已中斷連線的圖形端對端Disconnected graph End-to-end 1.0 (部分)1.0 (partial)

其他功能Other features

功能Feature EF6EF6 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 EF6EF6 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 EF6EF6 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 CoreFor 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: