EF Core.NET 命令列工具EF Core .NET Command-line Tools

Entity Framework Core.NET 命令列工具是跨平台擴充dotnet命令,也就是組件的.NET Core SDKThe Entity Framework Core .NET Command-line Tools are an extension to the cross-platform dotnet command, which is part of the .NET Core SDK.

提示

如果您使用 Visual Studio,我們建議您PMC 工具而是因為它們提供更加整合的體驗。If you're using Visual Studio, we recommend the PMC Tools instead since they provide a more integrated experience.

安裝工具Installing the tools

注意

.NET Core SDK 版本 2.1.300 和更新版本包含dotnet ef與 EF Core 2.0 和更新版本相容的命令。The .NET Core SDK version 2.1.300 and newer includes dotnet ef commands that are compatible with EF Core 2.0 and later versions. 因此如果您使用最新版.NET Core SDK 和 EF Core 執行階段,不需安裝作業,而且您可以忽略此章節的其餘部分。Therefore if you are using recent versions of the .NET Core SDK and the EF Core runtime, no installation is required and you can ignore the rest of this section.

相反地, dotnet ef工具包含在.NET Core SDK 版本 2.1.300 及更新版本不相容的 EF Core 版本 1.0 和 1.1 版。On the other hand, the dotnet ef tool contained in .NET Core SDK version 2.1.300 and newer is not compatible with EF Core version 1.0 and 1.1. 您可以使用.NET Core sdk 2.1.300 的電腦使用的 EF Core 這些舊版本的專案或較新的安裝之前,您也必須安裝 2.1.200 版本或更舊版本的 sdk 和設定應用程式使用該較舊的版本修改其 global.json檔案。Before you can work with a project that uses these earlier versions of EF Core on a computer that has .NET Core SDK 2.1.300 or newer installed, you must also install version 2.1.200 or older of the SDK and configure the application to use that older version by modifying its global.json file. 此檔案通常會包含在方案目錄 (一個以上專案) 中。This file is normally included in the solution directory (one above the project). 然後您可以繼續使用下面的 installlation 指令。Then you can proceed with the installlation instruction below.

舊版的.NET Core SDK,您可以安裝 EF Core.NET 命令列工具使用下列步驟:For previous versions of the .NET Core SDK, you can install the EF Core .NET Command-line Tools using these steps:

  1. 編輯專案檔,並加入 Microsoft.EntityFrameworkCore.Tools.DotNet 為 DotNetCliToolReference 項目 (如下所示)Edit the project file and add Microsoft.EntityFrameworkCore.Tools.DotNet as a DotNetCliToolReference item (See below)

  2. 執行下列命令:Run the following commands:

    dotnet add package Microsoft.EntityFrameworkCore.Design
    dotnet restore
    

產生的專案看起來應該像這樣:The resulting project should look something like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design"
                      Version="2.0.0"
                      PrivateAssets="All" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet"
                            Version="2.0.0" />
  </ItemGroup>
</Project>

注意

使用的套件參考PrivateAssets="All"表示它不公開給專案參考此專案中,這特別適用於通常只能用在開發期間的套件。A package reference with PrivateAssets="All" means it isn't exposed to projects that reference this project, which is especially useful for packages that are typically only used during development.

如果您這樣做的所有項目權限,您應該能夠成功地在命令提示字元中執行下列命令。If you did everything right, you should be able to successfully run the following command in a command prompt.

dotnet ef

使用的工具Using the tools

每當您叫用命令時,有兩個專案包含:Whenever you invoke a command, there are two projects involved:

目標專案就是新增 (在某些情況下會移除) 任何檔案的位置。The target project is where any files are added (or in some cases removed). 目標專案預設為目前的目錄中的專案,但可以使用來變更 --project 選項。The target project defaults to the project in the current directory, but can be changed using the --project option.

啟始專案是工具在執行您的專案程式碼時所模擬的專案。The startup project is the one emulated by the tools when executing your project's code. 它也會預設為目前的目錄中的專案,但可以使用來變更**--startup-project** 選項。It also defaults to the project in the current directory, but can be changed using the --startup-project option.

注意

比方說,更新 web 應用程式的 EF Core 安裝在不同的專案中的資料庫看起來會像這樣: dotnet ef database update --project {project-path} (從您的 web 應用程式目錄)For instance, updating the database of your web application that has EF Core installed in a different project would look like this: dotnet ef database update --project {project-path} (from your web app directory)

常見的選項:Common options:

--json 顯示 JSON 輸出。Show JSON output.
-c-c --context <DBCONTEXT> 要使用的 DbContext。The DbContext to use.
-p-p --project <PROJECT> 要使用的專案。The project to use.
-s-s --startup-project <PROJECT> 要使用的啟始專案。The startup project to use.
--framework <FRAMEWORK> 目標 framework。The target framework.
--configuration <CONFIGURATION> 要使用的組態。The configuration to use.
--runtime <IDENTIFIER> 要使用的執行階段使The runtime to use.
-h-h --help 顯示說明資訊。Show help information.
-v-v --verbose 顯示詳細資訊輸出。Show verbose output.
--no-color 沒有以色彩標示輸出。Don't colorize output.
--prefix-output 輸出層級的前置詞。Prefix output with level.

提示

指定 ASP.NET Core 環境,請設定ASPNETCORE_ENVIRONMENT之前執行的環境變數。To specify the ASP.NET Core environment, set the ASPNETCORE_ENVIRONMENT environment variable before running.

命令Commands

dotnet ef 資料庫卸除dotnet ef database drop

卸除資料庫。Drops the database.

選項:Options:

-f-f --force 未確認。Don't confirm.
--dry-run 顯示哪個資料庫會卸除,但不會卸除它。Show which database would be dropped, but don't drop it.

dotnet ef 資料庫更新dotnet ef database update

將資料庫更新為指定的移轉。Updates the database to a specified migration.

引數:Arguments:

<MIGRATION> 目標移轉。The target migration. 如果為 0,會還原所有移轉。If 0, all migrations will be reverted. 預設為最後一個移轉。Defaults to the last migration.

dotnet ef dbcontext 資訊dotnet ef dbcontext info

取得 DbContext 類型的相關資訊。Gets information about a DbContext type.

dotnet ef dbcontext 清單dotnet ef dbcontext list

列出可用的 DbContext 類型。Lists available DbContext types.

dotnet ef dbcontext scaffolddotnet ef dbcontext scaffold

建立資料庫的 DbContext 和實體類型。Scaffolds a DbContext and entity types for a database.

引數:Arguments:

<CONNECTION> 資料庫連接字串。The connection string to the database.
<PROVIDER> 要使用提供者The provider to use. (例如 Microsoft.EntityFrameworkCore.SqlServer)。(for example, Microsoft.EntityFrameworkCore.SqlServer)

選項:Options:

-d-d --data-annotations 使用屬性來設定模型 (如果可能的話)。Use attributes to configure the model (where possible). 如果省略,則只會使用 fluent API。If omitted, only the fluent API is used.
-c-c --context <NAME> DbContext 的名稱。The name of the DbContext.
--context-dir <PATH> 若要將 DbContext 檔案放在目錄。The directory to put DbContext file in. 路徑是相對於專案目錄。Paths are relative to the project directory.
-f-f --force 覆寫現有的檔案。Overwrite existing files.
-o-o --output-dir <PATH> 將檔案放入目錄。The directory to put files in. 路徑是相對於專案目錄。Paths are relative to the project directory.
--schema <SCHEMA_NAME>... 要產生的實體類型的資料表結構描述。The schemas of tables to generate entity types for.
-t-t --table <TABLE_NAME>...--table <TABLE_NAME>... 要產生的實體類型的資料表。The tables to generate entity types for.
--use-database-names 直接使用資料庫的資料表與資料行名稱。Use table and column names directly from the database.

新增 dotnet ef 移轉dotnet ef migrations add

加入新的移轉。Adds a new migration.

引數:Arguments:

<NAME> 移轉名稱。The name of the migration.

選項:Options:

-o-o --output-dir <PATH> --output-dir <PATH> 目錄 (及子命名空間) 使用。The directory (and sub-namespace) to use. 路徑是相對於專案目錄。Paths are relative to the project directory. 預設為 「 移轉 」。Defaults to "Migrations".

dotnet ef migrations 清單dotnet ef migrations list

列出可用的移轉。Lists available migrations.

dotnet ef migrations 移除dotnet ef migrations remove

移除最後一個移轉。Removes the last migration.

選項:Options:

-f-f --force 如果套用至資料庫,請還原移轉。Revert the migration if it has been applied to the database.

dotnet ef migrations 指令碼dotnet ef migrations script

產生 SQL 指令碼從移轉。Generates a SQL script from migrations.

引數:Arguments:

<FROM> 在開始移轉。The starting migration. 預設值為 0 (初始資料庫)。Defaults to 0 (the initial database).
<TO> 結束的移轉。The ending migration. 預設為最後一個移轉。Defaults to the last migration.

選項:Options:

-o-o --output <FILE> 要將結果寫入其中的檔案。The file to write the result to.
-i-i --idempotent 產生可在任何移轉隨時用在資料庫的指令碼。Generate a script that can be used on a database at any migration.