Generating MigrationHistory table in reverse engineered code first

c# ef-migrations entity-framework entity-framework-6

Question

I have reverse engineered on existing database for code first. Next i Enabled-Migrating for the context (Code based migration). When i create an initial add-migration it works fine and would apply on an empty database. But my requirement is that i need to use the same database i used for creating the models because of the data it has.

Now the conundrum is how do i implement the code based migrations. My database does not have a migration history table. So, when i run Update-database, it tries to create the existing tables and fails.

How can i capture the current state in the migration history or instruct EF to create the migration history with the current schema as the starting point.

Do i need to turn on the automatic migration for initial setup. Please suggest.

Note: I am using EF 6.

1
1
4/27/2017 3:16:37 PM

Accepted Answer

You need to establish a baseline migration of the existing items. So the first migration you create should be:

add-migration Initial -IgnoreChanges

The ignore changes tells EF to just save a snapshot of the current database. Now any subsequent migrations will not include the existing tables (just the changes). This would allow you to continue updating your existing database since EF will see the record in __MigrationHistory or deploy to a new, empty database.

See the under the hood section here.

1
4/27/2017 7:21:18 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow