Can we Scaffold DbContext from selected tables of an existing database

86,853

Solution 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   

Solution 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

Solution 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

Solution 4

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

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.

Share:
86,853
nam
Author by

nam

Updated on January 13, 2022

Comments

  • nam
    nam over 2 years

    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.