John
John

Reputation: 1764

EF 5.0 Migrations on a reverse engineered database. Can't update model changes because the tables already exist

I am using Beta 3 of EF power tools for EF5.0 to reverse engineer an existing database.

When I select "Reverse engineer code first" from the project context menu, I get all the models and the DBContexts + mapping as expected. And all looks good.

I enabled Migrations successfully immediately after the reverse engineering process completed.

However I want to add a new property to one of the models. After adding the new property,

I run PM> Add-Migration AddMyPropertyToMyTable

a Migration file is created,

If I then try PM> Update-Database

I get an error telling me that the tables already exist.

I am following the tutorial here:> http://msdn.microsoft.com/en-us/data/jj200620

Why am I getting this error? of course the table exists, I just reverse engineered it

Am I supposed to delete the database after reverse engineering? Or in the case of a reverse engineered Db, do I have to make my changes to the actual database and just reveres engineer it again to get the desired changes in my project (so what's the point of reverse engineering in the first place?)

is there something missing from the tutorial, i.e. an extra step required to make the database updateable after model changes?

Upvotes: 3

Views: 1645

Answers (1)

devdigital
devdigital

Reputation: 34349

When you enabled migrations with the existing database, EF didn't add the __MigrationHistory table or initial migration (DbMigration) file.

You can add an initial migration by using the following in the package manager console:

Add-Migration Initial -IgnoreChanges

This will be an empty initial migration. Then to force EF to create the __MigrationHistory table, you can use:

update-database

This should then create the __MigrationHistory table (under System Tables)

You should now be able to make model changes, and create new migration files (or use automatic migrations by configuring it in your Configuration.cs file under the Migrations folder).

You can run these migration changes by hand by using update-database, or have the database automatically migrated to the latest migration on application startup by using the MigrateDatabaseToLatestVersion initializer.

You can set this in the app.config/web.config so that it isn't set in production for example.

Upvotes: 8

Related Questions