天天看點

ASP.NET Core 學習記錄 - EFCore MySql DBFirst 生成實體類

作者:老碼農
單獨生成實體類,再拷貝到

1、建立ef類庫(設定一下命名空間namespace,與開發業務系統項目的一緻),同時将項目設為啟動項目,安裝2個Nuget包,如下所示:

Microsoft.EntityFrameworkCore.Tools
Pomelo.EntityFrameworkCore.MySql           

2、生成資料庫的實體和ef的DbContext對象,在項目根目錄下,用到的是 scaffold-dbcontext 指令;

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql"           

執行完成之後會生成指定的是Model ,

注意:表必須有主鍵,才會生成,如果沒有主機會報 unable to generate entity type for table “xxxxxxxx”的警告,當然實體也不會生成出現的問題:如果有表字段為 datetime類型的,生成的時候會報錯 應輸入辨別符,處理方法:把.()去掉。

3、輸出到指定檔案目錄(Models)下:

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql" -OutputDir Models           

4、加 -f 強制覆寫檔案

scaffold-dbcontext -force  "server=****;user id=root;password=****;database=****" -provider "pomelo.entityframeworkcore.mysql" -OutputDir Models -f           

5、檢視反向生成指令幫助:

dotnet ef dbcontext scaffold -h           

結果:

Usage: dotnet ef dbcontext scaffold [arguments] [options]

Arguments:
  <CONNECTION>  The connection string to the database.
  <PROVIDER>    The provider to use. (E.g. Microsoft.EntityFrameworkCore.SqlServer)

Options:
  -d|--data-annotations                  Use attributes to configure the model (where possible). If omitted, only the fluent API is used.
  -c|--context <NAME>                    The name of the DbContext. Defaults to the database name.
  --context-dir <PATH>                   The directory to put the DbContext file in. Paths are relative to the project directory.
  -f|--force                             Overwrite existing files.
  -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|--table <TABLE_NAME>...             The tables to generate entity types for.
  --use-database-names                   Use table and column names directly from the database.
  --json                                 Show JSON output. Use with --prefix-output to parse programatically.
  -n|--namespace <NAMESPACE>             The namespace to use. Matches the directory by default.
  --context-namespace <NAMESPACE>        The namespace of the DbContext class. Matches the directory by default.
  --no-onconfiguring                     Don't generate DbContext.OnConfiguring.
  --no-pluralize                         Don't use the pluralizer.
  -p|--project <PROJECT>                 The project to use. Defaults to the current working directory.
  -s|--startup-project <PROJECT>         The startup project to use. Defaults to the current working directory.
  --framework <FRAMEWORK>                The target framework. Defaults to the first one in the project.
  --configuration <CONFIGURATION>        The configuration to use.
  --runtime <RUNTIME_IDENTIFIER>         The runtime to use.
  --msbuildprojectextensionspath <PATH>  The MSBuild project extensions path. Defaults to "obj".
  --no-build                             Don't build the project. Intended to be used when the build is up-to-date.
  -h|--help                              Show help information
  -v|--verbose                           Show verbose output.
  --no-color                             Don't colorize output.
  --prefix-output                        Prefix output with level.
           
對應中文注釋:
<CONNECTION>資料庫連接配接字元串。
<PROVIDER>要使用的提供程式。(例如。Pomelo.EntityFrameworkCore.MySql)
選項:
-d |——資料注釋使用屬性來配置模型(如果可能)。如果省略,則隻使用fluent API。
-c |——context<NAME>DbContext的名稱。
--context dir<PATH>放置DbContext檔案的目錄。路徑是相對于項目目錄的。
-f |——強制覆寫現有檔案。
-o |——output dir<PATH>要放入檔案的目錄。路徑是相對于項目目錄的。
--模式<schema\u NAME>。。。要為其生成實體類型的表的架構。
-t |——表格<表格名稱>。。。要為其生成實體類型的表。
--使用資料庫名稱直接從資料庫中使用表名和列名。
--json顯示json輸出。
-p |——project<project>要使用的項目。
-啟動項目<project>要使用的啟動項目。
--framework<framework>目标架構。
--配置<配置>要使用的配置。
--runtime<runtime_IDENTIFIER>要使用的運作時。
--msbuildprojectextensionspath<PATH>MSBuild項目擴充路徑。預設為“obj”。
--不生成不生成項目。僅當生成是最新的時才使用此選項。
-h |--help顯示幫助資訊
-v |——verbose顯示詳細輸出。
--無顔色不給輸出着色。
--使用級别字首output prefix output。           

(此處已添加書籍卡片,請到今日頭條用戶端檢視)

(此處已添加紀錄片卡片,請到今日頭條用戶端檢視)