Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update model after scaffolding existing database EF core 2.X

I am using EF core 2.X scaffolding existing database. I have generated models classes using "dotnet ef dbcontext scaffold" command and it generate model classes.

Database team has change some table I have to run "dotnet ef dbcontext scaffold" command again to generate model classes to pick only changes.

for example let say I have one table called "employee" has column id, name.

I run "dotnet ef dbcontext scaffold" to generate models

After that I changed employee table and add one more column called "address" in database. How can I scaffold command to pick changes only .

Note: I know after generating models I should use migration to change database but our db team is has changed db and unfortunately, I have to do this. and advice

like image 822
itmannz Avatar asked Nov 07 '22 21:11

itmannz


1 Answers

You can provide an optional parameter to the scaffolding command to update only the table you target.

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DirectoryNameOfYourModels -Tables employee -f

If you are using .net core cli then use.

dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o DirectoryNameOfYourModels -t employee -f
like image 72
Amir Avatar answered Nov 27 '22 23:11

Amir