從 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.PostgresNpgSql.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

命名空間Namespaces

套件名稱以及命名空間已從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

若要使用的名稱是重大的功能性變更,我們採用 rc2DbSet<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(...):

services.AddEntityFramework()
  .AddSqlServer()
  .AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));

在 RC2 中,您可以移除呼叫AddEntityFramework()AddSqlServer()等等。:In RC2, you can remove the calls to AddEntityFramework(), AddSqlServer(), etc.:

services.AddDbContext<ApplicationDbContext>(options =>
  options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));

您也需要加入建構函式,為您衍生內容,取得內容的選項,並將其傳遞給基底建構函式。You also need to add a constructor, to your derived context, that takes context options and passes them to the base constructor. 這被必要由於我們移除了偷偷在幕後嚇人最特別的部分: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)
{
}

IServiceProvider 中傳遞Passing 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.

測試Testing

執行此作業最常見的案例是測試時,控制 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:

services.AddEntityFrameworkSqlServer()
  .AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
    options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"])
           .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 ef命令 ASP.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,因為.NET CLI 要取代的 DNX。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": [
      "portable-net45+win8+dnxcore50",
      "portable-net45+win8"
    ]
  }
}

提示

如果您使用 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

如果您在 Visual Studio 中的封裝管理員主控台中使用 Entity Framework 命令,則您必須確定您已安裝的 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 中使用 「 匯入 」Using "imports" in project.json

其中一些 EF 核心相依性不支援.NET 標準尚未。Some of EF Core's dependencies do not support .NET Standard yet. EF 核心標準.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 標準相容的 framework,即使它們不是。This forces NuGet to treat this binaries that match this provide as a compatible framework with .NET Standard, even though they are not. 雖然"可攜式 net451 + win8"不是與標準.NET 相容的 100%,它是相容的 PCL 從轉換為.NET 標準。Although "portable-net451+win8" is not 100% compatible with .NET Standard, it is compatible enough for the transition from PCL to .NET Standard. 當以.NET 標準最後升級 EF 的相依性,則可以移除匯入。Imports can be removed when EF's dependencies eventually upgrade to .NET Standard.

在陣列語法時,可以加入多個架構 」 匯入 」。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.