從 EF Core 1.0 RC1 升級至 1.0 RC2Upgrading from EF Core 1.0 RC1 to 1.0 RC2

本文章提供移動至 RC2 RC1 封裝建置的應用程式的指引。This article provides guidance for moving an application built with the RC1 packages to RC2.

封裝名稱和版本Package Names and Versions

之間 RC1 和 RC2,我們變更為 「 Entity Framework Core 」 從 「 Entity Framework 7 」。Between RC1 and RC2, we changed from "Entity Framework 7" to "Entity Framework Core". 您可以深入了解之變更的原因由 Scott hanselman 主講的這篇文章You can read more about the reasons for the change in this post by Scott Hanselman. 這項變更,因為我們的套件名稱則是從變更EntityFramework.*Microsoft.EntityFrameworkCore.*和我們的版本,從7.0.0-rc1-final1.0.0-rc2-final(或1.0.0-preview1-final工具)。Because of this change, our package names changed from EntityFramework.* to Microsoft.EntityFrameworkCore.* and our versions from 7.0.0-rc1-final to 1.0.0-rc2-final (or 1.0.0-preview1-final for tooling).

您必須完全移除 RC1 封裝,然後再安裝 RC2 的。You will need to completely remove the RC1 packages and then install the RC2 ones. 以下是一些常見的套件的對應。Here is the mapping for some common packages.

RC1 封裝RC1 Package RC2 的對等項目RC2 Equivalent
EntityFramework.MicrosoftSqlServer 7.0.0-rc1-finalEntityFramework.MicrosoftSqlServer 7.0.0-rc1-final Microsoft.EntityFrameworkCore.SqlServer 1.0.0-rc2-finalMicrosoft.EntityFrameworkCore.SqlServer 1.0.0-rc2-final
EntityFramework.SQLite 7.0.0-rc1-finalEntityFramework.SQLite 7.0.0-rc1-final Microsoft.EntityFrameworkCore.Sqlite 1.0.0-rc2-finalMicrosoft.EntityFrameworkCore.Sqlite 1.0.0-rc2-final
EntityFramework7.Npgsql 3.1.0-rc1-3EntityFramework7.Npgsql 3.1.0-rc1-3 NpgSql.EntityFrameworkCore.Postgres NpgSql.EntityFrameworkCore.Postgres
EntityFramework.SqlServerCompact35 7.0.0-rc1-finalEntityFramework.SqlServerCompact35 7.0.0-rc1-final EntityFrameworkCore.SqlServerCompact35 1.0.0-rc2-finalEntityFrameworkCore.SqlServerCompact35 1.0.0-rc2-final
EntityFramework.SqlServerCompact40 7.0.0-rc1-finalEntityFramework.SqlServerCompact40 7.0.0-rc1-final EntityFrameworkCore.SqlServerCompact40 1.0.0-rc2-finalEntityFrameworkCore.SqlServerCompact40 1.0.0-rc2-final
EntityFramework.InMemory 7.0.0-rc1-finalEntityFramework.InMemory 7.0.0-rc1-final Microsoft.EntityFrameworkCore.InMemory 1.0.0-rc2-finalMicrosoft.EntityFrameworkCore.InMemory 1.0.0-rc2-final
EntityFramework.IBMDataServer 7.0.0-beta1EntityFramework.IBMDataServer 7.0.0-beta1 尚無法使用 rc2Not yet available for RC2
EntityFramework.Commands 7.0.0-rc1-finalEntityFramework.Commands 7.0.0-rc1-final Microsoft.EntityFrameworkCore.Tools 1.0.0-preview1-finalMicrosoft.EntityFrameworkCore.Tools 1.0.0-preview1-final
EntityFramework.MicrosoftSqlServer.Design 7.0.0-rc1-finalEntityFramework.MicrosoftSqlServer.Design 7.0.0-rc1-final Microsoft.EntityFrameworkCore.SqlServer.Design 1.0.0-rc2-finalMicrosoft.EntityFrameworkCore.SqlServer.Design 1.0.0-rc2-final


從變更封裝名稱以及命名空間Microsoft.Data.Entity.*Microsoft.EntityFrameworkCore.*Along with package names, namespaces changed from Microsoft.Data.Entity.* to Microsoft.EntityFrameworkCore.*. 您可以處理這項變更,使用 尋找/取代的using Microsoft.Data.Entityusing Microsoft.EntityFrameworkCoreYou can handle this change with a find/replace of using Microsoft.Data.Entity with using Microsoft.EntityFrameworkCore.

命名慣例變更的資料表Table Naming Convention Changes

我們在 RC2 中的重大功能變更,是使用名稱DbSet<TEntity>作為資料表名稱指定之實體的屬性對應,而非只是類別名稱。A significant functional change we took in RC2 was to use the name of the DbSet<TEntity> property for a given entity as the table name it maps to, rather than just the class name. 您可以深入了解這項變更相關的公告問題You can read more about this change in the related announcement issue.

對於現有的 RC1 應用程式,我們建議將下列程式碼新增至開始您OnModelCreating方法,以保留 RC1 命名策略:For existing RC1 applications, we recommend adding the following code to the start of your OnModelCreating method to keep the RC1 naming strategy:

foreach (var entity in modelBuilder.Model.GetEntityTypes())
    entity.Relational().TableName = entity.DisplayName();

如果您想要採用新的命名策略,我們建議已順利完成的升級步驟,然後移除的程式碼和建立移轉至適用於資料表的其餘部分會重新命名。If you want to adopt the new naming strategy, we would recommend successfully completing the rest of the upgrade steps and then removing the code and creating a migration to apply the table renames.

AddDbContext / Startup.cs 變更 (ASP.NET Core 專案)AddDbContext / Startup.cs Changes (ASP.NET Core Projects Only)

在 RC1,您必須將 Entity Framework 的服務加入應用程式服務提供者- Startup.ConfigureServices(...):In RC1, you had to add Entity Framework services to the application service provider - in Startup.ConfigureServices(...):

  .AddDbContext<ApplicationDbContext>(options =>

Rc2,您可以移除對AddEntityFramework()AddSqlServer()等等。:In RC2, you can remove the calls to AddEntityFramework(), AddSqlServer(), etc.:

services.AddDbContext<ApplicationDbContext>(options =>

您也需要將建構函式,加入至您的衍生內容,取得內容的選項,並將它們傳遞給基底建構函式。You also need to add a constructor, to your derived context, that takes context options and passes them to the base constructor. 由於我們移除了一些 scary 偷偷在幕後的魔力,會需要此項目:This is needed because we removed some of the scary magic that snuck them in behind the scenes:

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    : base(options)

由傳入 IServiceProviderPassing in an IServiceProvider

如果您有將傳遞的 RC1 代碼IServiceProvider內容,這現已移至DbContextOptions,而不會出現不同的建構函式參數。If you have RC1 code that passes an IServiceProvider to the context, this has now moved to DbContextOptions, rather than being a separate constructor parameter. 使用DbContextOptionsBuilder.UseInternalServiceProvider(...)設定服務提供者。Use DbContextOptionsBuilder.UseInternalServiceProvider(...) to set the service provider.


執行此動作最常見的案例是測試時控制 InMemory 資料庫的範圍。The most common scenario for doing this was to control the scope of an InMemory database when testing. 請參閱更新測試rc2 中執行此範例的文章。See the updated Testing article for an example of doing this with RC2.

解析內部的服務,從應用程式服務提供者 (ASP.NET Core 專案)Resolving Internal Services from Application Service Provider (ASP.NET Core Projects Only)

如果您有 ASP.NET Core 應用程式,您想要用來解析內部服務應用程式服務提供者的 EF 沒有多載AddDbContext,可讓您進行此設定:If you have an ASP.NET Core application and you want EF to resolve internal services from the application service provider, there is an overload of AddDbContext that allows you to configure this:

  .AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
           .UseInternalServiceProvider(serviceProvider)); );


我們建議您讓 EF,在內部管理自己的服務,除非您有理由,將內部的 EF 服務結合成您的應用程式服務提供者。We recommend allowing EF to internally manage its own services, unless you have a reason to combine the internal EF services into your application service provider. 您可能想要執行這項操作的主要原因是要使用您的應用程式服務提供者來取代 EF 會在內部使用的服務The main reason you may want to do this is to use your application service provider to replace services that EF uses internally

DNX 命令 = >.NET CLI (ASP.NET Core 專案)DNX Commands => .NET CLI (ASP.NET Core Projects Only)

如果您先前使用dnx efASP.NET 5 專案的命令,這些現在都已移至dotnet ef命令。If you previously used the dnx ef commands for ASP.NET 5 projects, these have now moved to dotnet ef commands. 相同的命令語法仍然適用。The same command syntax still applies. 您可以使用dotnet ef --help如語法的詳細資訊。You can use dotnet ef --help for syntax information.

註冊命令的方式已變更 rc2,由於 DNX.NET CLI 所取代。The way commands are registered has changed in RC2, due to DNX being replaced by .NET CLI. 命令現在會在中註冊tools一節中project.json:Commands are now registered in a tools section in project.json:

"tools": {
  "Microsoft.EntityFrameworkCore.Tools": {
    "version": "1.0.0-preview1-final",
    "imports": [


如果您使用 Visual Studio,您現在可以使用 Package Manager Console 來執行 ASP.NET Core 專案 (這不支援在 RC1 中) 的 EF 命令。If you use Visual Studio, you can now use Package Manager Console to run EF commands for ASP.NET Core projects (this was not supported in RC1). 您仍然需要註冊中的命令tools一節project.json若要這樣做。You still need to register the commands in the tools section of project.json to do this.

封裝管理員命令都需要 PowerShell 5Package Manager Commands Require PowerShell 5

如果您使用 Entity Framework 中的命令在 Visual Studio 中的套件管理員主控台,則您必須確定您已安裝的 PowerShell 5。If you use the Entity Framework commands in Package Manager Console in Visual Studio, then you will need to ensure you have PowerShell 5 installed. 這是將在下一步 的版本中移除的暫時需求 (請參閱發出 #5327如需詳細資訊)。This is a temporary requirement that will be removed in the next release (see issue #5327 for more details).

使用 project.json 中的"imports"Using "imports" in project.json

其中一些 EF Core相依性不支援.NET 標準尚未。Some of EF Core's dependencies do not support .NET Standard yet. EF Core 標準.NET 和.NET Core 專案中的,您可能需要加入 「 匯入 「 加入 project.json 暫時的解決方法。EF Core in .NET Standard and .NET Core projects may require adding "imports" to project.json as a temporary workaround.

新增 EF,當 NuGet 還原將會顯示這則錯誤訊息:When adding EF, NuGet restore will display this error message:

Package Ix-Async 1.2.5 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Ix-Async 1.2.5 supports:
  - net40 (.NETFramework,Version=v4.0)
  - net45 (.NETFramework,Version=v4.5)
  - portable-net45+win8+wp8 (.NETPortable,Version=v0.0,Profile=Profile78)
Package Remotion.Linq 2.0.2 is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package Remotion.Linq 2.0.2 supports:
  - net35 (.NETFramework,Version=v3.5)
  - net40 (.NETFramework,Version=v4.0)
  - net45 (.NETFramework,Version=v4.5)
  - portable-net45+win8+wp8+wpa81 (.NETPortable,Version=v0.0,Profile=Profile259)

因應措施是手動匯入 「 可攜式 net451 + win8"的可攜式設定檔。The workaround is to manually import the portable profile "portable-net451+win8". 因而不得不將符合此二進位檔的 NuGet 提供做為相容的架構與.NET Standard,即使它們不是。This forces NuGet to treat this binaries that match this provide as a compatible framework with .NET Standard, even though they are not. 雖然不是與.NET Standard 相容的 100%的 「 可攜式 net451 + win8",它是相容的 PCL 從轉換到.NET Standard。Although "portable-net451+win8" is not 100% compatible with .NET Standard, it is compatible enough for the transition from PCL to .NET Standard. 當 EF 的相依性最終升級至.NET Standard 時,就可以移除匯入。Imports can be removed when EF's dependencies eventually upgrade to .NET Standard.

多個架構可以新增至 「 imports 」,在陣列語法。Multiple frameworks can be added to "imports" in array syntax. 其他匯入可能需要額外的程式庫加入您的專案。Other imports may be necessary if you add additional libraries to your project.

  "frameworks": {
    "netcoreapp1.0": {
      "imports": ["dnxcore50", "portable-net451+win8"]

請參閱發出 #5176See Issue #5176.