asp.net-mvc 我们可以从现有数据库的选定表中构建 DbContext 吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39065769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:33:33  来源:igfitidea点击:

Can we Scaffold DbContext from selected tables of an existing database

asp.net-mvcentity-frameworkvisual-studio-2015asp.net-coreasp.net-mvc-scaffolding

提问by nam

As in previous versions of Entity Framework, is it possible in Entity Framework Core to reverse engineer only the selected tables of an existing database to create model classes out of them. This official ASP.NET sitereverse engineers the entire database. In past, as shown in this ASP.NET tutorial, using old EF you could reverse engineer only the selected tables/Views if you chose to.

与以前版本的 Entity Framework 一样,Entity Framework Core 是否可以仅对现有数据库的选定表进行逆向工程,以从中创建模型类。这个官方 ASP.NET 站点对整个数据库进行了逆向工程。过去,如本 ASP.NET 教程中所示,如果您选择使用旧的 EF,您只能对选定的表/视图进行逆向工程。

回答by Oleg

One can solve the problem by usage of dotnet ef dbcontext scaffoldcommand with multiple-t(--table) parameters. It allows to specify all the tables, which needed by imported (scaffolded). The feature is described initially here.

可以通过使用带有多个( ) 参数的dotnet ef dbcontext scaffold命令来解决该问题。它允许指定导入(脚手架)所需的所有表。该功能最初在此处描述。-t--table

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:

.NET 核心 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:

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   

回答by Lahiru Gamage

Force tag will update the existing selected models/files in the output directory.

Force 标签将更新输出目录中现有的选定模型/文件。

Scaffold-DbContext "Server=(localdb)\v11.0;Database=MyDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t User, Role -f

回答by R J

.NET Core CLI:

.NET 核心 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:

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

EF Core,MS SQL PM :

EF 核心,MS SQL 下午:

Scaffold-DbContext "server=PC\SQL2012;user=test;password=test123;database=student" Microsoft.EntityFrameworkCore.SqlServer -OutputDir student-Tables stu.names,stu.grades -f 

For more reference Visit entityframework-core-scaffold

更多参考请访问entityframework-core-scaffold