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">
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design"
                      PrivateAssets="All" />
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet"
                            Version="2.0.0" />


封裝參考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). 目標專案預設值為目前的目錄中的專案,但可以使用變更 -專案選項。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. 它也會預設為目前的目錄中的專案,但可以使用變更 -啟始專案選項。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 顯示 JSON 輸出。Show JSON output.
-c-c -內容<DBCONTEXT >--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> 目標 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--verbose 顯示詳細資訊輸出。Show verbose output.
--no-color--no-color 不要以色彩標示輸出。Don't colorize output.
--prefix-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.


dotnet ef 資料庫卸除dotnet ef database drop

卸除資料庫。Drops the database.


-f-f --force--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.


<移轉 ><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

Scaffold DbContext 類型及實體類型的資料庫。Scaffolds a DbContext and entity types for a database.


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


-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.
-內容-dir<路徑 >--context-dir <PATH> 將 DbContext 檔案放入目錄。The directory to put DbContext file in. 路徑是相對於專案目錄。Paths are relative to the project directory.
-f-f --force--force 覆寫現有檔案。Overwrite existing files.
-o-o -輸出 dir<路徑 >--output-dir <PATH> 將檔案放入目錄。The directory to put files in. 路徑是相對於專案目錄。Paths are relative to the project directory.
-結構描述<SCHEMA_NAME >...--schema <SCHEMA_NAME>... 要產生實體類型的資料表結構描述。The schemas of tables to generate entity types for.
-t-t -資料表<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.


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


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

dotnet ef 移轉清單dotnet ef migrations list

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

dotnet ef 移轉移除dotnet ef migrations remove

移除最後的移轉。Removes the last migration.


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

dotnet ef 移轉指令碼dotnet ef migrations script

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


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


-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.