Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update-Database tries to do an automatic migration even with automatic migrations disabled

I work on a team of 4 developers using EF5, everyone working on their own local database. Up until now we've been using automatic migrations but we're nearing the point where we need to release to production so we've disabled automatic migrations and started adding explicit code-based migrations.

Here is the problem: I ran the Update-Database command after a developer created a new explicit migration and I get the following error:

Applying code-based migrations: [201209080142319_CreatedDate.LastModifiedDate.Additions].
Applying code-based migration: 201209080142319_CreatedDate.LastModifiedDate.Additions.
Applying automatic migration:    201209080142319_CreatedDate.LastModifiedDate.Additions_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.

Why do I get this error even though I've disabled automatic migrations? I can fix this error by deleting the explicit migration and then re-scaffolding it (running Add-Migration). Then Update-Database runs fine and doesn't mention anything about 'Automatic migration...' Also, the code in the migration created by me when I run Add-Migration is identical to the one created by my teammate. I don't see why it would even try to do an automatic migration since AutomaticMigrationsEnabled = false;.

What am I missing here?

like image 664
Dave Graves Avatar asked Sep 09 '12 17:09

Dave Graves


People also ask

How do you set Dbmigragrationsconfiguration AutomaticMigrationsEnabled to true to 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. This error occurs when you have pending changes in your database migrations that have not been added yet.

How do I get rid of migration?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What is automatic migration in Entity Framework?

Automatic Migrations allows you to use Code First Migrations without having a code file in your project for each change you make. Not all changes can be applied automatically - for example column renames require the use of a code-based migration.

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.


1 Answers

I hate to answer my own question but I encountered this problem again. A developer on my team re-enabled automatic migrations on their local machine and then created an explicit migration, which reproduced this behavior as soon as I ran it.

Entity framework will always run an automatic migration before it runs an explicit migration that has the Source property defined in its .resx file, even if AutomaticMigrationsEnabled = false. An explicit migration will only have the Source property set if it is created after an automatic migration has been run.

The upshot is that disabling automatic migrations only means that EF won't automatically upgrade your schema when it detects model changes - but it might still do an automatic migration if it needs to fill in a gap between some explicit migrations. To avoid this behavior, don't use a mixture of automatic migrations and explicit migrations.

like image 106
Dave Graves Avatar answered Oct 19 '22 22:10

Dave Graves