Reputation: 1037
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?
Upvotes: 21
Views: 24782
Reputation: 714
@Dave Graves is right. I had the same issue and did his fix in order to disable the auto migration of particular migration file
heres some instructions:
202008181102535_BlogPost.cs
Source
property of it is being set by something like Resources.GetString("Source")
. replace that with null
and then try again to manually execute update-database
in your package manager console.Upvotes: 2
Reputation: 232
AutomaticMigrationEnabled = false
prevents your application from updating the database on its own.
But since your running Update-database yourself, Update-Database checks the current state of the database and then runs all the migration steps not already in the database including the changes in the model (dbContext) that do not have a code-based migration yet.
My guess is that there is a change in the model that would cause data loss.
You can use the -force
parameter to apply the changes still when there is data loss.
Upvotes: 0
Reputation: 1037
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.
Upvotes: 32
Reputation: 69
public class Configuration : DbMigrationsConfiguration<bailencasino.com.dal.Context.BlncnoContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = true;
}
Add AutomaticMigrationDataLossAllowed = true;
so you assume that you want to allow EF to add remove SQL objects that result in data loss.
Upvotes: 2
Reputation: 3492
My team has experienced something that may be related to this. If two team members both add a migration, check in their code, get latest, and then perform update-database, the second one will get an error because there is a migration "skipped" - their system sees the team member's migration was never implemented.
We've started checking everything in and getting latest, doing the update-database (if a team member added a new migration), then doing add-migration, update-database, check-in.
Upvotes: 0