Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we Scaffold DbContext from selected tables of an existing database

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.

like image 743
nam Avatar asked Aug 21 '16 15:08

nam


People also ask

When you use the scaffold-DbContext command which parameters are required?

You use the DbContext Scaffold command to generate the model. The command has two required arguments - a connection string and a provider.

For which database operation does the scaffolding?

ASP.NET Scaffolding is a code generation framework for ASP.NET Web applications. Visual Studio 2013 includes pre-installed code generators for MVC and Web API projects. You add scaffolding to your project when you want to quickly add code that interacts with data models.

Is scaffold-DbContext command is used in EF Code First approach?

The EF core only supports Code First & Database First approach. In Database First, We use the Scaffold-dbcontext to create the Model from an existing database. This is basically Reverse engineering the existing database. Once we create the entity classes databases first does not work.


1 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    
like image 151
Oleg Avatar answered Oct 03 '22 00:10

Oleg