Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 691
TheMook Avatar asked Aug 01 '13 09:08

TheMook


People also ask

How do I enable EF migration?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).

Does EF core support automatic migration?

Use the EF Core DB Context Service to automatically migrate database changes. An instance of the EF Core DB Context service is injected as a parameter into the Configure() method of the Startup. cs file, the DB Context instance is then used to apply any pending migrations to the database by calling the Database.

How do I update my EF database?

Let's use Update-Database to apply this migration to the database. Run the Update-Database command in a Package Manager console. Code First Migrations will compare the migrations in our Migrations folder with the ones that have been applied to the database.


2 Answers

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.

like image 130
BetaSystems - Rodrigo Duarte Avatar answered Sep 19 '22 13:09

BetaSystems - Rodrigo Duarte


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.

like image 37
JuChom Avatar answered Sep 20 '22 13:09

JuChom