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.
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.
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.
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:
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With