EF 核心.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

安裝 EF 核心.NET 命令列工具使用下列步驟: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.

常用選項:Common options:

--json--json 顯示 JSON 輸出。Show JSON output.
-c-c --context <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> 目標 framework。The target framework.
-設定<設定 >--configuration <CONFIGURATION> 要使用的組態。The configuration to use.
-執行階段<識別項 >--runtime <IDENTIFIER> 在執行階段使用。The runtime to use.
-h-h --help--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><PROVIDER> 若要使用提供者。The provider to use. (例如:(E.g. Microsoft.EntityFrameworkCore.SqlServer)Microsoft.EntityFrameworkCore.SqlServer)


-d-d --data-annotations--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.
-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-database-names 使用直接從資料庫資料表和資料行名稱。Use table and column names directly from the database.

dotnet ef 移轉新增dotnet ef migrations add

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


<NAME><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 不會檢查以查看是否移轉套用至資料庫。Don't check to see if the migration 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><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.