Can we Scaffold DbContext from selected tables of an existing database

纵饮孤独 提交于 2021-01-20 15:22:28

问题


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


回答1:


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

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   



回答2:


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

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



回答3:


.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

EF Core,MS SQL PM :

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



来源:https://stackoverflow.com/questions/39065769/can-we-scaffold-dbcontext-from-selected-tables-of-an-existing-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!