As per my last post, if you’re using Entity Framework with Azure Mobile Services you’ll want to move away from Automatic Database upgrades and move to Code Based migrations. There are several strategies posted online, here’s the strategy that worked best for our project.
One of the challenges that my team identified early in the process was that our database content changed more frequently than our database schema. The default database initializer strategies only run the database migration and seeder scripts when the schema changes, so we found it more useful to manually apply the database migrations on start-up. This adds a bit of performance overhead to the start-up process, but since our database seeder scripts were relatively small, this seemed like an acceptable trade-off.
First, you’ll want to enable Code Based Migrations if you haven’t already. This involves opening the Package Manager Console window and typing the following:
Enable-Migrations
This power-shell command will add a few additional NuGet packages and make a few changes to your project. Most notably, the script will add a Migrations folder to your project and a Configuration class. We’ll make a few changes to the Configuration class by allowing automatic migrations and allowing data loss. The data loss feature will allow you to drop database columns even if they have data, so pay attention to your migration scripts.
internal sealed class Configuration : DbMigrationsConfiguration<MyServiceContext> { public Configuration() { ContextKey = "MyProject.Models.MyServiceContext"; AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; } }
Next you’ll want to override the Seed method and populate your database with starter data. I highly recommend moving all your seeding logic to a static method so that you can reuse this logic in your test scripts, etc. Also note that the System.Data.Entity.Migrations namespace adds an AddOrUpdate extension method for DbSet<T> which greatly simplifies your database seed. Following this approach will allow you to run the seed as many times as you want without introducing duplicates.
protected override void Seed(MyServiceContext context) { // This method will always be called. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Nigel Tufnel" }, // new Person { FullName = "David St. Hubbins" }, // new Person { FullName = "Derek Smalls" } // ); // MyDatabaseSeeder.Seed(context); }
Finally, to apply the database migrations on start-up you’ll need to add the following to your WebApiConfig.Register() method:
public static void Register() { Database.SetInitializer<MyServiceContext>(null); var migrator = new DbMigrator(new Configuration()); migrator.Update(); }
With this in place, the DbMigrator will apply any database changes when there are available and reliably call the Seed method with every deployment.
Happy Coding.
No comments:
Post a Comment