安裝 Entity Framework CoreInstalling Entity Framework Core

必要條件Prerequisites

  • 若要開發以 .NET Core 2.1 為目標的應用程式,請安裝 .NET Core 2.1 SDKTo develop apps that target .NET Core 2.1, install the .NET Core 2.1 SDK. 即便您有最新版本的 Visual Studio 2017 也必須安裝該 SDK。The SDK has to be installed even if you have the latest version of Visual Studio 2017.

  • 若要使用 Visual Studio 開發以 .NET Core 2.1 為目標的應用程式,請安裝 Visual Studio 2017 15.7 版或更新版本。To use Visual Studio for development of apps that target .NET Core 2.1, install Visual Studio 2017 version 15.7 or later.

  • 若要在 ASP.NET Core 應用程式中使用 Entity Framework 2.1,請使用 ASP.NET Core 2.1。To use Entity Framework 2.1 in ASP.NET Core applications, use ASP.NET Core 2.1. 使用舊版 ASP.NET Core 的應用程式須更新至 2.1。Applications that use earlier versions of ASP.NET Core must be updated to 2.1.

  • 若為以 NET Framework 4.6.1 或更新版本為目標的應用程式,您可以使用 Visual Studio 2015。You can use Visual Studio 2015 for apps that target the .NET Framework 4.6.1 or later. 但是您需要能感知 .NET Standard 2.0 及其相容 Framework 的 NuGet 版本。But you need a version of NuGet that is aware of the .NET Standard 2.0 and its compatible frameworks. 若要在 Visual Studio 2015 中取得該項目,請將 NuGet 用戶端升級至 3.6.0 版To get that in Visual Studio 2015, upgrade the NuGet client to version 3.6.0.

取得 Entity Framework Core 執行階段Get the Entity Framework Core runtime

若要將 EF Core 執行階段程式庫新增至應用程式,請安裝您欲使用資料庫提供者適用的 NuGet 套件。To add EF Core runtime libraries to an application, install the NuGet package for the database provider you want to use. 如需支援的提供者和其 NuGet 套件名稱的清單,請查看資料庫提供者For a list of supported providers and their NuGet package names, see Database providers.

若要安裝或更新 NuGet 套件,請使用 .NET Core CLI、Visual Studio 套件管理員對話方塊,或是 Visual Studio 套件管理員主控台。To install or update NuGet packages, use the .NET Core CLI, the Visual Studio Package Manager Dialog, or the Visual Studio Package Manager Console.

若為 ASP.NET Core 2.1 應用程式,則會自動包含記憶體內部和 SQL Server 提供者,因此無須個別安裝。For ASP.NET Core 2.1 applications, the in-memory and SQL Server providers are automatically included, so there's no need to install them separately.

提示

如果您需要更新的應用程式是使用協力廠商資料庫提供者,則請務必檢查與您要使用之 EF Core 版本相容的提供者更新。If you need to update an application that is using a third-party database provider, always check for an update of the provider that is compatible with the version of EF Core you want to use. 例如,舊版的資料庫提供者與 2.1 版的 EF Core 執行階段不相容。For example, database providers for previous versions are not compatible with version 2.1 of the EF Core runtime.

.NET Core CLI.NET Core CLI

下列 .NET Core CLI 命令會安裝或更新 SQL Server 提供者:The following .NET Core CLI command installs or updates the SQL Server provider:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

您可在 dotnet add package 命令列使用 -v 修飾詞來指定特定版本。You can indicate a specific version in the dotnet add package command, using the -v modifier. 例如,若要安裝 EF Core 2.1.0 套件,請在命令附加 -v 2.1.0For example, to install EF Core 2.1.0 packages, append -v 2.1.0 to the command.

Visual Studio NuGet 套件管理員對話方塊Visual Studio NuGet Package Manager Dialog

  • 從功能表選取 [專案] > [管理 NuGet 套件]From the menu, select Project > Manage NuGet Packages

  • 按一下 [瀏覽] 或 [更新] 索引標籤Click on the Browse or the Updates tab

  • 若要安裝或更新 SQL Server 提供者,請選取 Microsoft.EntityFrameworkCore.SqlServer 套件並確認。To install or update the SQL Server provider, select the Microsoft.EntityFrameworkCore.SqlServer package, and confirm.

如需詳細資訊,請參閱 NuGet 套件管理員對話方塊For more information, see NuGet Package Manager Dialog.

Visual Studio NuGet 套件管理員主控台Visual Studio NuGet Package Manager Console

  • 從功能表選取 [工具] > [NuGet 套件管理員] > [套件管理員主控台]From the menu, select Tools > NuGet Package Manager > Package Manager Console

  • 若要安裝 SQL Server 提供者,請在套件管理員主控台中執行以下命令:To install the SQL Server provider, run the following command in the Package Manager Console:

    Install-Package Microsoft.EntityFrameworkCore.SqlServer
    
  • 若要更新提供者,請使用 Update-Package 命令。To update the provider, use the Update-Package command.

  • 若要指定特定版本,請使用 -Version 修飾詞。To specify a specific version, use the -Version modifier. 例如,若要安裝 EF Core 2.1.0 套件,請將 -Version 2.1.0 附加至命令For example, to install EF Core 2.1.0 packages, append -Version 2.1.0 to the commands

如需詳細資訊,請參閱套件管理員主控台For more information, see Package Manager Console.

取得 Entity Framework Core 工具Get Entity Framework Core tools

除了執行階段程式庫以外,您還可以安裝其他工具,在專案的設計期間中執行部分與 EF Core 相關的工作。Besides the runtime libraries, you can install tools that can perform some EF Core-related tasks in your project at design time. 例如,您可以建立移轉、套用移轉,以及利用現有的資料庫為基礎建立模型。For example, you can create migrations, apply migrations, and create a model based on an existing database.

有兩組工具可用:Two sets of tools are available:

雖然您可從套件管理員主控台使用 dotnet ef 命令,但在使用 Visual Studio 時使用套件管理員主控台工具更加方便:Although you can use the dotnet ef commands from the Package Manager Console, it's more convenient to use the Package Manager Console tools when you're using Visual Studio:

  • 它們會自動適用於目前在套件管理員主控台中選取的專案,而不需要您手動切換目錄。They automatically work with the current project selected in the Package Manager Console without requiring manually switching directories.
  • 在命令完成之後,它們會在 Visual Studio 中自動開啟命令所產生的檔案。They automatically open files generated by the commands in Visual Studio after the command is completed.

取得 CLI 工具Get the CLI tools

.NET Core SDK 雖包含 dotnet ef 命令,但您必須安裝 Microsoft.EntityFrameworkCore.Design 套件才能啟用命令:The dotnet ef commands are included in the .NET Core SDK, but to enable the commands you have to install the Microsoft.EntityFrameworkCore.Design package:

dotnet add package Microsoft.EntityFrameworkCore.Design 

若為 ASP.NET Core 2.1 應用程式,則已自動包含此套件。For ASP.NET Core 2.1 apps, this package is included automatically.

如同先前必要條件中所述,您也必須安裝 .NET Core 2.1 SDK。As explained earlier in Prerequisites, you also need to install the .NET Core 2.1 SDK.

重要

一律使用符合執行階段套件主要版本的工具套件版本。Always use the version of the tools package that matches the major version of the runtime packages.

取得套件管理員主控台套件Get the Package Manager Console tools

若要取得 EF Core 的套件管理員主控台工具,請安裝 Microsoft.EntityFrameworkCore.Tools 套件:To get the Package Manager Console tools for EF Core, install the Microsoft.EntityFrameworkCore.Tools package:

dotnet add package Microsoft.EntityFrameworkCore.Tools

若為 ASP.NET Core 2.1 應用程式,則已自動包含此套件。For ASP.NET Core 2.1 apps, this package is included automatically.

升級至 EF Core 2.1Upgrading to EF Core 2.1

若您要將現有的應用程式升級至 EF Core 2.1,則可能需要手動移除部分舊 EF Core 套件的參考:If you're upgrading an existing application to EF Core 2.1, some references to older EF Core packages may need to be removed manually:

  • EF Core 2.1 中不再需要或支援 Microsoft.EntityFrameworkCore.SqlServer.Design 等資料庫提供者設計階段套件,但在升級其他套件時不會自動將其移除。Database provider design-time packages such as Microsoft.EntityFrameworkCore.SqlServer.Design are no longer required or supported in EF Core 2.1, but aren't automatically removed when upgrading the other packages.

  • .NET CLI 工具現在包含在 .NET SDK 中,因此您可以從 .csproj 檔案移除對該套件的參考:The .NET CLI tools are now included in the .NET SDK, so the reference to that package can be removed from the .csproj file:

    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    

若應用程式的目標為 .NET Framework 且由舊版 Visual Studio 建立,請確認它們與 .NET Standard 2.0 程式庫相容:For applications that target the .NET Framework and were created by earlier versions of Visual Studio, make sure that they are compatible with .NET Standard 2.0 libraries:

  • 編輯專案檔,並確定下列項目出現在初始屬性群組中:Edit the project file and make sure the following entry appears in the initial property group:

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    
  • 針對測試專案,也請確定下列項目存在:For test projects, also make sure the following entry is present:

    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>