database first生成Model

Scaffold-DbContext命令

参数 描述
-Connection 数据库的连接字符串
-Provider 连接数据库的驱动,默认一般选择安装的Microsoft.EntityFrameworkCore.SqlServer
-OutputDir 生成的模型路径,路劲为启动项目的相对路劲
-ContextDir 生成的Context路径,路径为启动项目的相对路径
-Context 生成的Context名称
-Schemas <String[]> 选择数据的架构,默认选择所有。例如:(dbo.)
-Tables <String[]> 选择要生成的数据表,默认选择所有,也可以自行指定
-UseDatabaseNames 使用与数据库中显示的完全相同的表和列名称。如果省略此参数,则更改数据库名称以更符合C#名称样式约定。
-force 重新生成,覆盖掉已经创建的文件

例如:

sqlserver

  • 生成全部Model
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
  • 选择生成的Model
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables "Blog","Post" -ContextDir Context -Context BlogContext

mysql

官网地址:https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework-core-scaffold-example.html

Scaffolding a Database by Filtering Tables

It is possible to specify the exact tables in a schema to use when scaffolding database and to omit the rest. The command-line examples that follow show the parameters needed for filtering tables.

.NET Core CLI:

dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -o sakila -t actor -t film -t film_actor -t language -f

Package Manager Console in Visual Studio:

Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir Sakila -Tables actor,film,film_actor,language -f

Scaffolding with Multiple Schemas

When scaffolding a database, you can use more than one schema or database. Note that the account used to connect to the MySQL server must have access to each schema to be included within the context. Multiple-schema functionality was introduced in Connector/NET 6.10.3-rc and 8.0.9-dmr releases.

The following command-line examples show how to incorporate the sakila and world schemas within a single context.

.NET Core CLI:

dotnet ef dbcontext scaffold "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -o sakila --schema sakila --schema world -f

Package Manager Console in Visual Studio:

Scaffold-DbContext "server=localhost;port=3306;user=root;password=mypass;database=sakila" MySql.Data.EntityFrameworkCore -OutputDir Sakila -Schemas sa
posted @ 2020-02-11 19:21  Jonny-Xhl  阅读(91)  评论(0编辑  收藏  举报