TheMook
TheMook

Reputation: 1541

EF 5. "Automatic migration was not applied because it would result in data loss" - only changed the column name

I have:

AutomaticMigrationsEnabled = True
AutomaticMigrationDataLossAllowed = False

in my configuration file and the existing model is:

Public Property ID() As Integer
Public Property ERP_ArticleCode() As String
Public Property description() As String

All I did was change the 3rd column from "description" to "am_description" and ran "update-database -verbose" which resulted in "Automatic migration was not applied because it would result in data loss"!

I do not understand this... why can't I just change a column name and update the database - this shouldn't be a dataloss issue, should it? Am I doing something wrong?

Upvotes: 21

Views: 40829

Answers (6)

Bob Provencher
Bob Provencher

Reputation: 450

I've also seen this when I have a migration applied to my database that is in a parallel branch, that my current branch has no knowledge of and I attempt to revert to some older migration. The best solution there is to switch back to the authoring branch and revert to the latest common migration between the two branches.

Upvotes: 1

gneric
gneric

Reputation: 3725

My issue was that I just had another minor change that itself was a migration. While trying to update. Check it with Update-Database -Name "peek", just to see if you have too.

Upvotes: 1

dundideat
dundideat

Reputation: 63

You have to change the line

AutomaticMigrationDataLossAllowed = False

to

AutomaticMigrationDataLossAllowed = True

Note: take caution to use true only in your development environment as it can result in data loss after you've deployed for production.

Upvotes: -8

manuelmejiaio
manuelmejiaio

Reputation: 7017

I solved like this, just type:

Update-Database -Force

Upvotes: 0

JuChom
JuChom

Reputation: 5999

You should edit your up and down method and replace the AddColumn and DropColumn instructions with RenameColumn.

EF can't detect if you're renaming a column or if you want to delete one and create a new one.

Upvotes: 20

Rodrigo Duarte
Rodrigo Duarte

Reputation: 711

This code below isn't necessary to run your Migration:

AutomaticMigrationsEnabled = True
AutomaticMigrationDataLossAllowed = False

And, acording to this article (from entityframework codeplex), this is an EF error and you can ignore this with -Force attribute in your migration.

Update-Database -Force

or

Update-Database -TargetMigration: X -Force

It should resolve your problem.

IMO, you should let EF decide what does with your columns, just my opinion.

Upvotes: 38

Related Questions