安裝 EF CoreInstalling EF Core

必要條件Prerequisites

若要開發 .NET Core 2.0 應用程式 (包含目標設為 .NET Core 的 ASP.NET Core 2.0 應用程式),您將需要下載並安裝您平台適用的 .NET Core 2.0 SDK 版本。In order to develop .NET Core 2.0 applications (including ASP.NET Core 2.0 applications that target .NET Core) you will need to download and install a version of the .NET Core 2.0 SDK that is appropriate to your platform. 即使您已安裝 Visual Studio 2017 15.3 版,也是如此。This is true even if you have installed Visual Studio 2017 version 15.3.

若要搭配使用 EF Core 2.0 或任何其他 .NET Standard 2.0 程式庫與 .NET 平台和 .NET Core 2.0 (例如與 .NET Framework 4.6.1 或更新版本),您需要可辨識 .NET Standard 2.0 和其相容架構的 NuGet 版本。In order to use EF Core 2.0 or any other .NET Standard 2.0 library with a .NET platforms besides .NET Core 2.0 (e.g. with .NET Framework 4.6.1 or greater) you will need a version of NuGet that is aware of the .NET Standard 2.0 and its compatible frameworks. 以下是您可以取得這個項目的一些方法:Here are a few ways you can obtain this:

使用舊版 Visual Studio 所建立且目標設為 .NET Framework 的專案可能需要進行額外修改,才能與 .NET Standard 2.0 程式庫相容:Projects created with previous versions of Visual Studio and targeting .NET Framework may need additional modifications in order to be 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>
    

取得位元Getting the bits

將 EF Core 執行階段程式庫新增至應用程式的建議方式,是從 NuGet 安裝 EF Core 資料庫提供者。The recommended way to add EF Core runtime libraries into an application is to install an EF Core database provider from NuGet.

除了執行階段程式庫之外,您還可以安裝工具;在設計階段,更輕鬆地於專案中執行數個 EF Core 相關工作,例如建立和套用移轉,以及根據現有資料庫來建立模型。Besides the runtime libraries, you can install tools which make it easier to perform several EF Core-related tasks in your project at design time, such as creating and applying migrations, and creating a model based on an existing database.

提示

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

提示

除了協力廠商資料庫提供者之外,將目標設為 ASP.NET Core 2.0 的應用程式還可以使用 EF Core 2.0,而且沒有其他相依性。Applications targeting ASP.NET Core 2.0 can use EF Core 2.0 without additional dependencies besides third party database providers. 將目標設為 ASP.NET Core 舊版本的應用程式需要升級至 ASP.NET Core 2.0,才能使用 EF Core 2.0。Applications targeting previous versions of ASP.NET Core need to upgrade to ASP.NET Core 2.0 in order to use EF Core 2.0.

使用 .NET Core 命令列介面 (CLI) 的跨平台開發Cross-platform development using the .NET Core Command Line Interface (CLI)

若要開發目標設為 .NET Core 的應用程式,您可以選擇搭配使用 dotnet CLI 命令 與慣用的文字編輯器或整合式開發環境 (IDE) (例如 Visual Studio、Visual Studio for Mac 或 Visual Studio Code)。To develop applications that target .NET Core you can choose to use the dotnet CLI commands in combination with your favorite text editor, or an Integrated Development Environment (IDE) such as Visual Studio, Visual Studio for Mac or Visual Studio Code.

重要

目標設為 .NET Core 的應用程式需要特定 Visual Studio 版本 (例如 .NET Core 1.x 開發需要 Visual Studio 2017),而 .NET Core 2.0 開發需要 Visual Studio 2017 15.3 版。Applications that target .NET Core require specific versions of Visual Studio, e.g. .NET Core 1.x development requires Visual Studio 2017, while .NET Core 2.0 development requires Visual Studio 2017 version 15.3.

若要在跨平台 .NET Core 應用程式中安裝或升級 SQL Server 提供者,請切換至應用程式的目錄,並在命令列中執行下列命令:To install or upgrade the SQL Server provider in a cross-platform .NET Core application, switch to the application's directory and run the following in a command line:

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

您可以使用 -v 修飾詞,在 dotnet add package 命令中指出的特定版本安裝。You can indicate a specific version install in the dotnet add package command, using the -v modifier. 例如E.g. 安裝 EF Core 2.0 套件、將 -v 2.0.0 附加至命令。to install EF Core 2.0 packages, append -v 2.0.0 to the command.

EF Core 包含 dotnet CLI 的一組其他命令,並從 dotnet ef 開始。EF Core includes a set of additional commands for the dotnet CLI, starting with dotnet ef. 若要使用 dotnet ef CLI 命令,您應用程式的 .csproj 檔案必須包含下列項目:In order to use the dotnet ef CLI commands, your application’s .csproj file needs to contain the following entry:

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

EF Core 的 .NET Core CLI 工具也需要稱為 Microsoft.EntityFrameworkCore.Design 的個別套件。The .NET Core CLI tools for EF Core also require a separate package called Microsoft.EntityFrameworkCore.Design. 只需要使用下列命令,即可將它新增至專案:You can simply add it to the project using:

dotnet add package Microsoft.EntityFrameworkCore.Design

重要

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

Visual Studio 開發Visual Studio development

您可以使用 Visual Studio 開發許多不同類型的應用程式,而這些應用程式的目標設為 .NET Core、.NET Framework 或 EF Core 所支援的其他平台。You can develop many different types of applications that target .NET Core, .NET Framework, or other platforms supported by EF Core using Visual Studio.

從 Visual Studio,您可以有兩種方式可以將 EF Core 資料庫提供者新增至應用程式:There are two ways you can install an EF Core database provider in your application from Visual Studio:

使用 NuGet 的套件管理員使用者介面Using NuGet's Package Manager User Interface

  • 在功能表上,選取 [專案] > [管理 NuGet 套件]Select on the menu Project > Manage NuGet Packages

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

  • 選取 Microsoft.EntityFrameworkCore.SqlServer 套件和所需的版本,並確認Select the Microsoft.EntityFrameworkCore.SqlServer package and the desired version and confirm

使用 NuGet 的套件管理員主控台 (PMC)Using NuGet's Package Manager Console (PMC)

  • 在功能表上,選取 [工具] > [NuGet 套件管理員] > [套件管理員主控台]Select on the menu Tools > NuGet Package Manager > Package Manager Console

  • 在 PMC 中鍵入和執行下列命令:Type and run the following command in the PMC:

    Install-Package Microsoft.EntityFrameworkCore.SqlServer
    
  • 您可以改成使用 Update-Package 命令來更新已安裝為較新版本的套件You can use the Update-Package command instead to update a package that is already installed to a more recent version

  • 若要指定特定版本,您可以使用 -Version 修飾詞;例如,安裝 EF Core 2.0 套件、將 -Version 2.0.0 附加至命令To specify a specific version, you can use the -Version modifier, e.g. to install EF Core 2.0 packages, append -Version 2.0.0 to the commands

工具Tools

在 Visual Studio 中,也有 PowerShell 版本且在 PMC 內執行的 EF Core 命令,而其功能與 dotnet ef 命令類似。There is also a PowerShell version of the EF Core commands which run inside the PMC in Visual Studio, with similar capabilities to the dotnet ef commands. 若要使用這些項目,請使用套件管理員 UI 或 PMC 來安裝 Microsoft.EntityFrameworkCore.Tools 套件。In order to use these, install the Microsoft.EntityFrameworkCore.Tools package using either the Package Manager UI or the PMC.

重要

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

提示

在 Visual Studio 中,雖然可以從 PMC 使用 dotnet ef 命令,但是使用 PowerShell 版本更為方便:Although it is possible to use the dotnet ef commands from the PMC in Visual Studio, it is far more convenient to use the PowerShell version:

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

重要

EF Core 2.0 中已被取代的套件:如果您要將現有應用程式升級至 EF Core 2.0,則可能需要手動移除舊 EF Core 套件的一些參考。Deprecated packages in EF Core 2.0: If you are upgrading an existing application to EF Core 2.0, some references to older EF Core packages may need to be removed manually. 特別的是,EF Core 2.0 不再需要或支援資料庫提供者設計階段套件 (例如 Microsoft.EntityFrameworkCore.SqlServer.Design),但在升級其他套件時不會自動予以移除。In particular, database provider design-time packages such as Microsoft.EntityFrameworkCore.SqlServer.Design are no longer required or supported in EF Core 2.0, but will not be automatically removed when upgrading the other packages.