Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategy for Getting SQL for AutomaticMigrations

We're using EF 5RC, code first with migrations. I feel like this should be an easy question to answer (i hope). Is there a good way to figure out what the automatic migration is attempting to do.

I've added a migration through the Add-Migration PS command. I've invoked Update-Database and all seems fine with that migration. Now - I'm just running Update-Database like i normally do, but with the following error:

PM> update-database -Verbose
Using StartUp project 'Web'.
Using NuGet project 'DataAccess'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'UserGroup' (DataSource: (localdb)\v11.0, Provider: System.Data.SqlClient, Origin: Configuration).
No pending code-based migrations.
Applying automatic migration: 201206301526422_AutomaticMigration.
Automatic migration was not applied because it would result in data loss.

Notice, i'm adding the -Verbose option, and I've tried it again with the -Script. But I have no idea what we're migrating to; and what SQL - or what it thinks will result in data loss.

I do not want to simply enable "allow data loss" here, but am trying to understand how to troubleshoot these migrations.

Thank you in advance!

like image 582
cbkadel Avatar asked Jun 30 '12 15:06

cbkadel


2 Answers

Just run:

PM> Update-Database -Script -Force

This will generate the SQL and display it in a window without running it.

like image 122
Diego Mijelshon Avatar answered Oct 15 '22 03:10

Diego Mijelshon


I got this error on Azure after a publish , but there u cant use -Force , so global solution (and no need for -Force on local too )

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true; // <-- THIS LINE
}
like image 27
Zakos Avatar answered Oct 15 '22 03:10

Zakos