M_Idrees
M_Idrees

Reputation: 2172

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.

Upvotes: 14

Views: 30477

Answers (2)

MiguelSlv
MiguelSlv

Reputation: 15103

Scaffold-DbContext can be used with option -Context to expand the current DbContext file, instead of creating a new one.

Example:

Scaffold-DbContext "Server=server;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -Context MyDbContext

The generated code for the MyDbContext will be placed on a partial class file.

Upvotes: 0

Julián
Julián

Reputation: 1386

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

Upvotes: 16

Related Questions