cbkadel
cbkadel

Reputation: 264

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!

Upvotes: 9

Views: 5411

Answers (2)

Zakos
Zakos

Reputation: 1509

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
}

Upvotes: 9

Diego Mijelshon
Diego Mijelshon

Reputation: 52745

Just run:

PM> Update-Database -Script -Force

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

Upvotes: 18

Related Questions