比較 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. EF6 在 .NET Framework 4.x 上執行,這表示只能在 Windows 上執行。EF6 runs on the .NET Framework 4.x, which means it runs only on Windows.

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 或 .NET Framework 上執行。EF Core is a cross-platform product that can run on .NET Core or .NET Framework.

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 (query types) 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

查詢資料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
混合用戶端/伺服器評估Mixed client/server evaluation 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: Non-entity types (query 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

儲存資料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.0 (1)1.0 (1)
SQLiteSQLite [是]Yes 1.01.0
SQL Server CompactSQL Server Compact [是]Yes 1.0 (2)1.0 (2)
DB2DB2 [是]Yes 1.01.0
FirebirdFirebird [是]Yes 2.02.0
Jet (Microsoft Access)Jet (Microsoft Access) 2.0 (2)2.0 (2)
記憶體內部 (測試用)In-memory (for testing) 1.01.0

1 Oracle 目前有付費提供者。1 There is currently a paid provider available for Oracle. Oracle 的免費官方提供者正在開發中。A free official provider for Oracle is being worked on.

2 SQL Server Compact 和 Jet 提供者只適用於 .NET Framework (不適用於 .NET Core)。2 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 (主控台、WinForms、WPF、ASP.NET).NET Framework (Console, WinForms, WPF, ASP.NET) [是]Yes 1.01.0
.NET Core (主控台、ASP.NET Core).NET Core (Console, ASP.NET Core) 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: