比較 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 (部分;#214)1.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 PartialPartial 待處理項目 (#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
可為 Null 的參考型別 (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
以文字為基礎的查詢語言 (Entity 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
變更追蹤:ProxyChange 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)
已中斷連線的圖形低階 APIDisconnected graph low-level APIs Poor 1.01.0
已中斷連線的圖形端對端Disconnected graph End-to-end 1.0 (部分;#5536)1.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 (部分;#626)3.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 CoreSee Porting from EF6 to EF Core for more information.