Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update existing model class generated by Scaffold-DbContext

Working with ASP.NET CORE EF, I have generated model classes from existing database with following command:

Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Now my database is changed, for example I added a new column in a table AppUser. I don't want to use migrations to keep sync models with database.

I already made changes in database and now I want to update my existing model classes from current database state. Any suggestion how to do this?

Should I delete my existing model classes and regenerate by using the same command Scaffold-DbContext or do we have any other way make existing model classes to reflect new changes.

like image 658
M_Idrees Avatar asked Nov 20 '19 11:11

M_Idrees


People also ask

How to scaffold Entity Framework model classes and dbcontext?

By default Entity model classes and DbContext class will be scaffolded into the project’s root directory. By default, the ‘ Context’ folder will be used to store Context classes and the ‘Models’ folder will be used to store model classes. If you want to provide Custom Model Folder use the below command.

How to force scaffolding to update existing model files?

When you force a scaffold update you will loose any modification you have made to the model file not that I can think of why anyone would do that. You can use Scaffold-DbContext command with -force flag. This way you can force scaffolding to overwrite existing model files. Replace ConnectionString & TableName as per your requirements.

How do I use scaffold dbcontext in Visual Studio?

Scaffold-DbContext Command. Use Scaffold-DbContext to create a model based on your existing database. The following parameters can be specified with Scaffold-DbContext in Package Manager Console: In Visual Studio, select menu Tools -> NuGet Package Manger -> Package Manger Console and run the following command:

How to update only the table that the scaffolding target?

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.


1 Answers

Are you looking for something like this?

As far as I know, to update the model you must execute the same command to overwrite the changes but with another additional flag.

I remember using the -f (force) option to overwrite the changes:

Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f

Although it is also possible to indicate which entity you want to update (table):

Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t <table> -f
like image 168
Julián Avatar answered Nov 15 '22 11:11

Julián