Apply Reverse engineering on a subset of tables using Entity Framework Core 6

70 views Asked by At

I'm developing an ASP.NET Core Web API (.NET 6) and I want to apply reverse engineering to only certain tables. Because the database is rather extensive, and I don't need all the tables, just some of them.

Currently, I'm creating the models manually, and I'd like to know if there is a command to perform reverse engineering specifying the tables I want to include. I'm working with SQL Server.

In order to perform reverse engineering, I'm executing the following command in the Package Manager Console::

PM> Scaffold-DbContext "Server=.\SQLExpress;Database=mydatabase;Trusted_Connection=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

This command generates the entities for all the existing tables in my database.

1

There are 1 answers

0
Daniela Pinto On

I have found the solution to this issue, and I think it could be useful for someone else, so I'm sharing it here.

I resolved it by adding the table names of the entities I wanted to have in my DbContext within the PM command.

Instead of using this command:

PM> Scaffold-DbContext "Server=.\SQLExpress;Database=mydatabase;Trusted_Connection=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

I used the following:

PM> Scaffold-DbContext "Server=.\SQLExpress;Database=mydatabase;Trusted_Connection=true;" Microsoft.EntityFrameworkCore.SqlServer -t Clientes,Libros,Ventas -o Models

And, by making that small change, I managed to get only the tables I wanted to have in my app.