EF Code First with existing database on another database

c# ef-migrations entity-framework-6


The title is a bit confusing but I'm trying to specify that the problem is not initiating a code first model and migration for an existing database, but comes after that.

I needed to activate automatic migration because we switched to a code first model for our system. So, here's what has been done:

  1. I created an empty InitialCreate for the existing database
  2. I did some other scripts because there were some changes, those worked OK and the scripts were created and run on database

The problem happen when I want to use those script and migrate another database that was not yet initialized this way. I don't know what to do.

When I try to run Update-database I get the error:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.

If I do a Add-Migration it creates a migration with everything in it again, all the create tables, it's like it ignores my currents scripts, the fact that there is a InitialCreate empty and the other scripts.

3/5/2018 8:23:03 PM

Accepted Answer

OK, you have 2 databases - let's say DEV and PROD. Both are in an identical state and have the same schema before migrations have been enabled. This is what to do:

1 - Add migrations to your DEV environment and set your database initializer to MigrateDatabaseToLatestVersion. Another option is to programatically run migrations.

// take a snapshot of current state. -IgnoreChanges prevents recreate of existing objects.
add-migration InitialBaseline -IgnoreChanges

2 - There are several ways to keep the other database(s) in sync:

A) Maintain migrations in parallel by changing the connection string. So point at PROD, and run update-database to create the __MigrationHistory table and apply the initial, blank, baseline. I don't recommend this option for PROD databases (see below).

B) Sync with scripts. Many organizations don't want EF applying changes and instead require DBAs to apply scripts. For this option, you may want to set your database initializer to NULL. In this case you can do an update-database -Script to generate changes. This would be done on migrations subsequent to your initial baseline since they are already in sync. See here for more info on this technique.

C) Use database projects or a diff tool to keep things in sync.

Now when you go and change your models in DEV:

add-migration Changes1

For option A, change connect string and repeat. For option B, use update-database -Script. For option C, resync with tool.

NOTE: "I needed to activate automatic migrations..." - Automatic migrations are a whole different matter and can complicate the process. See here.

3/6/2018 6:11:46 PM

Related Questions


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