Can we Scaffold DbContext from selected tables of an existing database

asp.net MvcEntity FrameworkVisual Studio-2015asp.net Coreasp.net Mvc-Scaffolding

asp.net Mvc Problem Overview


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.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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   

Solution 2 - asp.net Mvc

> 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

Solution 3 - asp.net Mvc

.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

Solution 4 - asp.net Mvc

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

Solution 5 - asp.net Mvc

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.

Solution 6 - asp.net Mvc

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnamView Question on Stackoverflow
Solution 1 - asp.net MvcOlegView Answer on Stackoverflow
Solution 2 - asp.net MvcLahiru GamageView Answer on Stackoverflow
Solution 3 - asp.net MvcR JView Answer on Stackoverflow
Solution 4 - asp.net MvcCh UsmanView Answer on Stackoverflow
Solution 5 - asp.net MvcdevOps_7View Answer on Stackoverflow
Solution 6 - asp.net MvcTom TrnkaView Answer on Stackoverflow