Search code examples
entity-frameworkasp.net-coreef-database-first

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.


Solution

  • 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