Reputation: 221
I have an MVC .NET backend for an Azure Mobile App, where I'm employing Entity Framework V6 Code First Migrations, however, when Publishing it does not mark the Migrations as having been deployed but it does deploy them to the Azure SQL Database. So, if I make a change to my model, I have to manually delete the migration in the Migration folder and then Add-Migration again, or I get a Pending Migration error (below). Rinse and repeat for every change. Shouldn't the Publish Web program mark these migrations as having been performed?
I've followed the tutorial here: https://azure.microsoft.com/en-in/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/
In my Startup.MobileApp.cs File, I initially use the below code to get everything synced up, and it works;
// Use Entity Framework Code First to create database tables based on your DbContext
Database.SetInitializer(new MyInitializer());
public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
protected override void Seed(MyContext context)
{
base.Seed(context);
}
}
This works too; Enabling-Migrations, Adding a Migration, changing the Startup.MobileApp.cs File to point to the Configuration file, checking the Execute Code-First Migrations in the Publish Web program, and then Publishing:
// Using Code First Migration, so no need for Initializer unless reseeding or resetting database.
var migrator = new DbMigrator(new Migrations.Configuration());
migrator.Update();
internal sealed class Configuration : DbMigrationsConfiguration<MyService.Models.MyContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
}
}
However, if I make a change to my model, and then try to add another migration to the migration folder I get this error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201608181434049_Name]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
But if I manually delete the Migration, then I can add a new migration, and the publisher runs and backend database is updated correctly.
Upvotes: 2
Views: 201
Reputation: 8035
Don't check the box for applying migrations.
In your App_Start\Startup.MobileApp.cs
, remove the DbInitializer() call and replace it with:
var migrator = new DbMigrator(new Migrations.Configuration());
migrator.Update();
For my article on the subject, see: https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter3/server/
Upvotes: 4