移植到 EF Core EF6 EDMX 型模型Porting an EF6 EDMX-Based Model to EF Core

EF Core 不支援模型的 EDMX 檔案格式。EF Core does not support the EDMX file format for models. 連接埠這些模型的最佳選項是從您的應用程式的資料庫中產生新的程式碼為基礎的模型。The best option to port these models, is to generate a new code-based model from the database for your application.

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

安裝Microsoft.EntityFrameworkCore.ToolsNuGet 套件。Install the Microsoft.EntityFrameworkCore.Tools NuGet package.

重新產生模型Regenerate the model

您現在可以使用反向工程功能來建立模型,根據您現有的資料庫。You can now use the reverse engineer functionality to create a model based on your existing database.

在 Package Manager Console 執行下列命令 (工具 –> NuGet 套件管理員 –> Package Manager Console)。Run the following command in Package Manager Console (Tools –> NuGet Package Manager –> Package Manager Console). 請參閱套件管理員主控台 (Visual Studio)命令選項,以建立結構的資料表等子集。See Package Manager Console (Visual Studio) for command options to scaffold a subset of tables etc.

Scaffold-DbContext "<connection string>" <database provider name>

例如,以下是命令來建立模型,以從您的 SQL Server LocalDB 執行個體上的部落格資料庫。For example, here is the command to scaffold a model from the Blogging database on your SQL Server LocalDB instance.

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer

移除 EF6 模型Remove EF6 model

您現在會從您的應用程式移除 EF6 模型。You would now remove the EF6 model from your application.

它,就可以保留 EF6 NuGet 封裝 (EntityFramework) 安裝,因為 EF Core 和 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.

更新您的程式碼Update your code

此時,它是定址的編譯錯誤,並檢閱看 EF6 和 EF Core 之間的行為變更會影響您的程式碼。At this point, it's a matter of addressing compilation errors and reviewing code to see if the behavior changes between EF6 and EF Core will impact you.

測試連接埠Test the port

只在您的應用程式會編譯,因為並不表示它成功匯出至 EF Core。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.

提示

請參閱開始使用 ASP.NET Core 與現有的資料庫在 EF Core對於如何使用現有的資料庫,其他參考See Getting Started with EF Core on ASP.NET Core with an Existing Database for an additional reference on how to work with an existing database,