移植到 EF Core EF6 程式碼式模型Porting an EF6 Code-Based Model to EF Core

如果您已閱讀所有警告,而且您準備好要連接埠,以下是一些可協助您開始指導方針。If you've read all the caveats and you are ready to port, then here are some guidelines to help you get started.

安裝 EF Core NuGet 封裝Install EF Core NuGet packages

若要使用 EF 核心,您可以安裝 NuGet 封裝您想要使用的資料庫提供者。To use EF Core, you install the NuGet package for the database provider you want to use. 例如,當目標為 SQL Server,您會安裝Microsoft.EntityFrameworkCore.SqlServerFor example, when targeting SQL Server, you would install Microsoft.EntityFrameworkCore.SqlServer. 請參閱資料庫提供者如需詳細資訊。See Database Providers for details.

如果您打算使用的移轉,則您也應該安裝Microsoft.EntityFrameworkCore.Tools封裝。If you are planning to use migrations, then you should also install the Microsoft.EntityFrameworkCore.Tools package.

它,就可以保留 EF6 NuGet 封裝 (EntityFramework) 安裝,因為 EF 核心和 EF6 可以使用-並存相同的應用程式中。It is fine to leave the EF6 NuGet package (EntityFramework) installed, as EF Core and EF6 can be used side-by-side in the same application. 不過,如果您不打算使用 EF6 應用程式的任何區域中,然後解除安裝封裝有助於讓上需要注意的程式碼片段的編譯錯誤。However, if you aren't intending to use EF6 in any areas of your application, then uninstalling the package will help give compile errors on pieces of code that need attention.

交換命名空間Swap namespaces

大部分應用程式開發介面使用 EF6 位於System.Data.Entity命名空間 (及相關的子命名空間)。Most APIs that you use in EF6 are in the System.Data.Entity namespace (and related sub-namespaces). 第一個程式碼變更為以交換Microsoft.EntityFrameworkCore命名空間。The first code change is to swap to the Microsoft.EntityFrameworkCore namespace. 您會通常會以您衍生的內容的程式碼檔案開頭,然後算出從該處發生時解決編譯錯誤。You would typically start with your derived context code file and then work out from there, addressing compilation errors as they occur.

內容組態 (連線等)。Context configuration (connection etc.)

中所述確保 EF 核心會用於您的應用程式,EF 核心有較少神奇偵測連接到資料庫。As described in Ensure EF Core Will Work for Your Application, EF Core has less magic around detecting the database to connect to. 您必須覆寫OnConfiguring上您的衍生的內容,並設定資料庫的連接使用資料庫提供者特定 API 方法。You will need to override the OnConfiguring method on your derived context, and use the database provider specific API to setup the connection to the database.

大部分的 EF6 應用程式的應用程式中儲存連接字串App/Web.config檔案。Most EF6 applications store the connection string in the applications App/Web.config file. 在 EF 核心您讀取此連接字串使用ConfigurationManager應用程式開發介面。In EF Core, you read this connection string using the ConfigurationManager API. 您可能需要將參考加入System.Configurationframework 組件,可以使用此 API。You may need to add a reference to the System.Configuration framework assembly to be able to use this API.

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);
    }
}

更新您的程式碼Update your code

此時,它是解決編譯錯誤,並檢閱查看如果的行為變更會影響您的程式碼。At this point, it's a matter of addressing compilation errors and reviewing code to see if the behavior changes will impact you.

現有的移轉Existing migrations

此外並非真正 EF 核心 EF6 移轉現有的可行方式。There isn't really a feasible way to port existing EF6 migrations to EF Core.

可能的話,最好假設 EF6 從所有先前移轉已經套用到資料庫並再開始移轉結構描述從該點使用 EF 核心。If possible, it is best to assume that all previous migrations from EF6 have been applied to the database and then start migrating the schema from that point using EF Core. 若要這樣做,您可以使用Add-Migration命令來新增移轉後的模型移植到 EF 核心。To do this, you would use the Add-Migration command to add a migration once the model is ported to EF Core. 然後,您會移除所有的程式碼從UpDownscaffold 移轉的方法。You would then remove all code from the Up and Down methods of the scaffolded migration. 該初始移轉已建立結構時,後續的移轉將會比較模型。Subsequent migrations will compare to the model when that initial migration was scaffolded.

測試連接埠Test the port

只在您的應用程式會編譯,因為並不表示它成功匯出至 EF 核心。Just because your application compiles, does not mean it is successfully ported to EF Core. 您必須測試您的應用程式,以確保沒有任何行為變更已經造成不利影響您的應用程式的所有區域。You will need to test all areas of your application to ensure that none of the behavior changes have adversely impacted your application.