AsValeO
AsValeO

Reputation: 3039

Entity Framework 7 Update DbContext after migration

I've got an existing DB and new WPF project.

1) I scaffolded db context with Scaffold-DbContext and recieved DbContext.cs and a file for each entity that represents a DB tables. That's OK. Here is example of one of them:

public partial class SomeEntity
    {
        public long SomeEntityID { get; set; }
        public int SomeInt { get; set; }
    }

2) I've manually updated this entity (no changes in DbContext.cs) :

public partial class SomeEntity
    {
        public long SomeEntityID { get; set; }
        public int SomeInt { get; set; }
        public string SomeString { get; set; }
    }

3) I've used Add-Migration command. Now I've got a migration file that describes required changes and DbContextSnapshot.cs. Snapshot includes changes I've just made with SomeEntity. Database is not updated.

4) I've used Update-Database command. Database updated to match last migration. But my DbContext.cs doesn't know anything about changes in DB. It is still the same old good DbContext that I had after scaffolding DB.

Is this OK? Should I now change DbContext manually to match changes that I've made to my entities and applied them to DB? Is there a way to update DbContext.cs from DB after changes without 'rescaffolding'? (Because, for example, all comments and attributes in entity files will be lost, because they will be replaced with new ones if I use Scaffold-DbContext again)

Upvotes: 1

Views: 676

Answers (1)

ErikEJ
ErikEJ

Reputation: 41749

You will have to run scaffolding again, but keep in mind that the generated classes are partial, so you can put your changes in another file

Upvotes: 1

Related Questions