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?
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).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With