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.

- 21,967
- 37
- 158
- 332
-
1Look at [the old answer](http://stackoverflow.com/a/34457974/315935). It describes that one can use [dotnet ef dbcontext scaffold](https://docs.efproject.net/en/latest/miscellaneous/cli/dotnet.html#dotnet-ef-dbcontext-scaffold) with **multiple -t** parameters, which specifies the tables which need be scaffolded. – Oleg Aug 21 '16 at 15:41
-
@Oleg That should work - thank you. A reference to [Table selection in reverse engineering](https://github.com/aspnet/EntityFramework/wiki/Design-Meeting-Notes-(October-1,-2015)#table-selection-in-reverse-engineering) in your old post is an answer. For the benefit of other readers of this post, you may want to convert your comment to an answer; and I'll mark that as an answer. – nam Aug 21 '16 at 15:59
6 Answers
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

- 26,542
- 16
- 152
- 170

- 220,925
- 34
- 403
- 798
-
2This is a workaround but I have a decent size database where I would like to only generate object for existing tables and no views. When generated everything, I've finished up with 156 classes, not very interested in manual pick of dozens of tables to exclude views. – Vočko Jun 16 '20 at 06:14
-
@Vočko: If you examine the line (scroll right), which I posted, you will see that I suggest to use "-t" parameter **multiple times** with tables, which you need: "-t actor -t film -t film_actor -t language". As the result dotnet will scaffold the tables which we specified: "actor", "film", ... – Oleg Jun 16 '20 at 18:01
-
I know what you mean, but you didn't get my point. My database has about 60 tables and I would have to name every single one. Some dbs might have hundreds of tables. This approach simply doesn't work. I've requested the feature on EF Core GitHub and also was pointed to VS EF Core Power Tools that can split views and tables. Only works for VS users (Windows) though. – Vočko Jun 17 '20 at 00:16
-
@Vočko: Sorry, but I still can't follow you. Do you have some new question to me? Which one? You post your comment to my old answer on the question where the problem was: how to create EF model for selected (specific) tables of existing database instead of generating of EF model for all exiting tables (the default behavior). My answer is written before the first version of .NET Core was released. At the time no views were scaffold. – Oleg Jun 17 '20 at 11:40
-
@Vočko: I don't use .NET Core last years. I can suggest you to try use plugins like [this one](https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars) see [here](https://blog.tonysneed.com/2018/05/27/customize-ef-core-scaffolding-with-handlebars-templates/) or some close one. If I understand your problem correctly the approach could help you. – Oleg Jun 17 '20 at 11:42
-
-
And FYI both the dotnet core CLI command and the PS version will work in Package Manager console within VS – Mark Z. Oct 19 '22 at 13:22
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

- 849
- 5
- 14
- 27
.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

- 475
- 3
- 14
Package Manger Console (MySql)
Scaffold-DbContext "server=localhost;port=3306;user=root;password=yourpassword;database=sakila" MySql.EntityFrameworkCore -OutputDir Models -Tables actor,film,film_actor,language -f
Package Manager Console (MSSQL)
Scaffold-DbContext "Server=desktop-vd5sscb;Initial Catalog=databaseName;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f
Package Manager Console (Sqlite)
Scaffold-DbContext "data source = yourdbname" Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models -f
For Sqlite The default db dir is your project folder... where controller folders are located

- 519
- 6
- 9
Considering, If you have n number of tables, initially at design time, your database design architecture should group those tables in their suitable schemas
For eg: For Database “Company” you can have many tables, when you design database group these tables into schemas like: Users, ProductA, ProductB, ProductC etc
Then assuming you are working on ProductA tables only then you can simply add -Schemas flag and scaffold only tables in ProductA
Another example could be suppose you are working on authorisation based project only and you want to implement identity auth with ef, then you can simply scaffold “Users” schema instead of products and make your oAuth APIs work.
Scaffold-DbContext ... -Schemas Users
These are just few use cases where you can use scaffolding effectively.
-
This seems more practical to me rather than creating separate database for selected tables only to enjoy more clean approach while scaffolding dbcontext – muhammad tayyab Aug 30 '23 at 22:45
The parameter -Tables table1, table2, table3 works for me for more tables. The -o Model parameter is the output that creates the folder to which the model is generated. The -force parameter regenerates the model each time it is started, such as updating the database. The -Context DbE parameter renames the database context class.
Package manager console
Scaffold-DbContext name=ConnectionStrings:DbE Microsoft.EntityFrameworkCore.SqlServer -o Model -force -Tables T_Users_Of_Chat -Context DbE

- 56
- 4