1

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.

5
  • The Db your working against might be missing the _EFMIgrations table? Commented Mar 5, 2018 at 20:26
  • Well, yes, it does. That's the problem. How do I create them? Manually? I thought the update-database would do it? How will I do that in production? Commented Mar 5, 2018 at 20:29
  • It's going to depend on exactly which migration was applied previously and which one your trying to apply. I'm just guessing that it sounds like the Db that gives you the trouble is missing all or some of the migrations ran against the other database. Commented Mar 5, 2018 at 20:32
  • Database 1 is the one I configured the code first migrations first, it was an existing database. So it had no __migrationtables either. Database 2 is another database, that is ready also to have the code first initialized in it (like a production database), it also doesn't have the __migration tables, I don't know how to initialize it, update-database doesnt work, neither add-migration Commented Mar 5, 2018 at 20:34
  • @JSteward I'm not sure I was clear on my problem, I have a dabatase that started without code first, then added code first with empty initial. Now, I want to convert another database with code first, I'm just unable to do it with the scripts I have Commented Mar 6, 2018 at 16:46

1 Answer 1

1

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.

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

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
update-database

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, automatic migrations are not what I meant, I meant code first. I will do some tests with the options you proposed. I'm the person responsible for the production, so I could chose the option on how to do it. What do you recommend?
Opinions vary. We have a full change management system with DBAs so we have to use scripts. It's a pain, but they deal with all the backups and restores. I know the Azure deployments have options to run migrations as well.
Just to specify, I had a bug in my configuration that made the migration not find the migration files in the project, this is corrected, so now I'm trying to implement a solution that will work for all the database, but your answer works, so I'll mark it as good! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.