安裝 Entity Framework CoreInstalling Entity Framework Core

必要條件Prerequisites

  • 因為 EF Core 是 .NET Standard 2.0 程式庫。EF Core is a .NET Standard 2.0 library. 所以 EF Core 需要支援 .NET Standard 2.0 的 .NET 實作才可執行。So EF Core requires a .NET implementation that supports .NET Standard 2.0 to run. 其他 .NET Standard 2.0 程式庫也可以參考 EF Core。EF Core can also be referenced by other .NET Standard 2.0 libraries.

  • 例如,您可以使用 EF Core 開發以 .NET Core 為目標的應用程式。For example, you can use EF Core to develop apps that target .NET Core. 建置 .NET Core 應用程式需要 .NET Core SDKBuilding .NET Core apps requires the .NET Core SDK. 此外,您也可以使用 Visual Studio、Visual Studio for Mac 或 Visual Studio Code 等開發環境。Optionally, you can also use a development environment like Visual Studio, Visual Studio for Mac, or Visual Studio Code. 如需詳細資訊,請參閱 .NET Core 使用者入門For more information, check Getting Started with .NET Core.

  • 您可以搭配 Visual Studio 使用 EF Core,來開發以 Windows 上 .NET Framework 4.6.1 或更新版本為目標的應用程式。You can use EF Core to develop applications that target .NET Framework 4.6.1 or later on Windows, using Visual Studio. 建議使用最新版本的 Visual Studio。The latest version of Visual Studio is recommended. 若您想要使用較舊版本,例如 Visual Studio 2015,請務必將 NuGet 用戶端升級至 3.6.0 版,以搭配 .NET Standard 2.0 程式庫使用。If you want to use an older version, like Visual Studio 2015, make sure you upgrade the NuGet client to version 3.6.0 to work with .NET Standard 2.0 libraries.

  • EF Core 可在其他的 .NET 實作上執行,像是 Xamarin 及 .NET Native。EF Core can run on other .NET implementations like Xamarin and .NET Native. 但實踐這些實作有執行階段的限制,其可能影響 EF Core 在應用程式上的運作。But in practice those implementations have runtime limitations that may affect how well EF Core works on your app. 如需詳細資訊,請參閱 EF Core 支援的 .NET 實作For more information, see .NET implementations supported by EF Core.

  • 最後,不同的資料庫提供者可能需要特定的資料庫引擎版本、.NET 實作或作業系統。Finally, different database providers may require specific database engine versions, .NET implementations, or operating systems. 請確認有可用且能為您應用程式支援正確環境的 EF Core 資料庫提供者可用。Make sure an EF Core database provider is available that supports the right environment for your application.

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

若要將 EF Core 新增至應用程式,請安裝您欲使用資料庫提供者適用的 NuGet 套件。To add EF Core to an application, install the NuGet package for the database provider you want to use.

若您要建置 ASP.NET Core 應用程式,就不必安裝記憶體內部及 SQL Server 提供者。If you're building an ASP.NET Core application, you don't need to install the in-memory and SQL Server providers. 目前版本的 ASP.NET Core 包含這些提供者以及 EF Core 執行階段。Those providers are included in current versions of ASP.NET Core, alongside the EF Core runtime.

若要安裝或更新 NuGet 套件,您可以使用 .NET Core 命令列介面 (CLI)、Visual Studio 套件管理員對話方塊,或是 Visual Studio 套件管理員主控台。To install or update NuGet packages, you can use the .NET Core command-line interface (CLI), the Visual Studio Package Manager Dialog, or the Visual Studio Package Manager Console.

.NET Core CLI.NET Core CLI

  • 請從作業系統的命令列使用以下 .NET Core CLI 命令,來安裝或更新 EF Core SQL Server 提供者:Use the following .NET Core CLI command from the operating system's command line to install or update the EF Core 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.2.0 套件,請將 -v 2.2.0 附加至命令。For example, to install EF Core 2.2.0 packages, append -v 2.2.0 to the command.

如需詳細資訊,請參閱 .NET Core 命令列介面 (CLI) 工具For more information, see .NET command-line interface (CLI) tools.

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

  • 從 Visual Studio 功能表選取 [專案] > [管理 NuGet 套件]From the Visual Studio 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

  • 從 Visual Studio 功能表選取 [工具] > [NuGet 套件管理員] > [套件管理員主控台]From the Visual Studio 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.2.0 套件,請將 -Version 2.2.0 附加至命令For example, to install EF Core 2.2.0 packages, append -Version 2.2.0 to the commands

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

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

您可以安裝工具以處理專案中與 EF Core 相關的工作,像是建立和套用資料庫移轉,或依據現有的資料庫建立 EF Core 模型。You can install tools to carry out EF Core-related tasks in your project, like creating and applying database migrations, or creating an EF Core model based on an existing database.

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

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

  • 它們會自動使用 Visual Studio 中 PMC 內目前的所選專案,而不必手動切換目錄。They automatically work with the current project selected in the PMC in Visual Studio, without requiring manually switching directories.

  • 在命令完成之後,它們會在 Visual Studio 中自動開啟命令所產生的檔案。They automatically open files generated by the commands in Visual Studio after the command is completed.

取得 .NET Core CLI 工具Get the .NET Core CLI tools

.NET Core CLI 工具需要先前在必要條件中提到的 .NET Core SDK。.NET Core CLI tools require the .NET Core SDK, mentioned earlier in Prerequisites.

目前版本的 .NET Core SDK 雖包含 dotnet ef 命令,但您必須安裝 Microsoft.EntityFrameworkCore.Design 套件才能啟用特殊專案上的命令:The dotnet ef commands are included in current versions of the .NET Core SDK, but to enable the commands on a specific project, you have to install the Microsoft.EntityFrameworkCore.Design package:

dotnet add package Microsoft.EntityFrameworkCore.Design 

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

重要

一律使用符合執行階段套件主要版本的工具套件版本。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. 例如,從 Visual Studio 安裝:For example, from Visual Studio:

Install-Package Microsoft.EntityFrameworkCore.Tools

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

升級為最新的 EF CoreUpgrading to the latest EF Core

  • 每當我們發行新版本的 EF Core,同時也會發行屬於 EF Core 專案一部分的新版本提供者,像是 Microsoft.EntityFrameworkCore.SqlServer、Microsoft.EntityFrameworkCore.Sqlite 和 Microsoft.EntityFrameworkCore.InMemory。Any time we release a new version of EF Core, we also release a new version of the providers that are part of the EF Core project, like Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Sqlite, and Microsoft.EntityFrameworkCore.InMemory. 您可以僅升級為新版本的提供者,來取得所有改善。You can just upgrade to the new version of the provider to get all the improvements.

  • EF Core、SQL Server 以及記憶體內部提供者都包含於目前版本的 ASP.NET Core 中。EF Core, together with the SQL Server and the in-memory providers are included in current versions of ASP.NET Core. 若要將現有的 ASP.NET Core 應用程式升級為 EF Core 的較新版本,請持續升級 ASP.NET Core 的版本。To upgrade an existing ASP.NET Core application to a newer version of EF Core, always upgrade the version of ASP.NET Core.

  • 如果您需要更新的應用程式是使用協力廠商資料庫提供者,則請務必檢查與您要使用之 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.0 版的 EF Core 執行階段不相容。For example, database providers for previous versions are not compatible with version 2.0 of the EF Core runtime.

  • EF Core 的協力廠商提供者通常不會同時發行修補程式版本與 EF Core 執行階段。Third-party providers for EF Core usually don't release patch versions alongside the EF Core runtime. 若要將使用協力廠商提供者的應用程式升級為 EF Core 的修補程式版本,您可能必須新增個別 EF Core 執行階段元件的直接參考,像是 Microsoft.EntityFrameworkCore 及 Microsoft.EntityFrameworkCore.Relational。To upgrade an application that uses a third-party provider to a patch version of EF Core, you may need to add a direct reference to individual EF Core runtime components, such as Microsoft.EntityFrameworkCore, and Microsoft.EntityFrameworkCore.Relational.

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

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

    • 從 2.1 版起,.NET CLI 工具都包含在 .NET SDK 中,因此您可以從專案檔移除該套件的參考:The .NET CLI tools are included in the .NET SDK since version 2.1, so the reference to that package can be removed from the project file:

      <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
      
  • 以 .NET Framework 為目標的應用程式可能必須變更為使用 .NET Standard 2.0 程式庫:Applications that target .NET Framework may need changes to work 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>