Aaron Anodide
Aaron Anodide

Reputation: 17186

How to tell EF Code First Migration system that database is updated

Here's the sequence of events that I've experienced a number of times:

Now I'm in a state where the context can't be used because it gives an error that it needs to be updated.

However, updating results in an error due to the coulmn already being there (duplicate column).

So am I just fundamentally "doing it wrong" or is there some command in the package manager that says, "I updated my database manually and it's now up to date"?

Upvotes: 0

Views: 395

Answers (2)

jjslagace
jjslagace

Reputation: 232

To prevent this from appening again and again, you should not add the column manually in the database but instead use update-database to update it.

The correct steps should be:

  • Add a property to DbContext corresponding to the column
  • Generate the a migration
  • Run update-database

Upvotes: 0

Mark Oreta
Mark Oreta

Reputation: 10416

What's happening is EF doesn't know your migration has been applied yet.

What I've done is in package manager console enter:

update-database -v -f -script

Which generates the script for the migration - then I copy the last line in the generated sql, which adds the data to the __MigrationHistory table, and execute it manually. It looks something like this:

enter image description here

This should sync up your code with the database.

Upvotes: 2

Related Questions