text: A .NET Developer's Toolbox
  Find new books and literate friends with Shelfari, the online book club.
  Tuesday, November 13, 2012
  #
  Now that Azure supports .NET Framework 4.5, you can use all the latest and greatest available features. A common scenario is to be able to use Entity Framework Code First Migrations with a SQL Database in Azure. Prior to Code First Migrations, Entity Framework provided database initializers. While convenient for demos and prototypes, database initializers weren’t useful for much beyond that because, if you delete and re-create your entire database when the schema changes, you lose all of your operational data. This is the void that Migrations are meant to fill. For example, if you add a column to your model, Migrations will alter the database to add the column rather than blowing away the entire database and re-creating it from scratch.
  Azure is becoming increasingly easier to use – especially with features like Azure Web Sites. Being able to use Entity Framework Migrations in Azure makes deployment easier than ever. In this blog post, I’ll walk through enabling Automatic Code First Migrations on Azure. I’ll use the Simple Membership provider for my example.
  First, we’ll create a new Azure Web site called “migrationstest” including creating a new SQL Database along with it:
  Next we’ll go to the web site and download the publish profile:
  In the meantime, we’ve created a new MVC 4 website in Visual Studio 2012 using the “Internet Application” template. This template is automatically configured to use the Simple Membership provider. We’ll do our initial Publish to Azure by right-clicking our project and selecting “Publish…”. From the “Publish Web” dialog, we’ll import the publish profile that we downloaded in the previous step:
  Once the site is published, we’ll just click the “Register” link from the default site. Since the AccountController is decorated with the [InitializeSimpleMembership] attribute, the initializer will be called and the initial database is created.
  We can verify this by connecting to our SQL Database on Azure with SQL Management Studio (after making sure that our local IP address is added to the list of Allowed IP Addresses in Azure):
  One interesting note is that these tables got created with the default Entity Framework initializer – which is to create the database if it doesn’t already exist. However, our database did already exist! This is because there is a new feature of Entity Framework 5 where Code First will add tables to an existing database as long as the target database doesn’t contain any of the tables from the model.
  At this point, it’s time to enable Migrations. We’ll open the Package Manger Console and execute the command:
  PM> Enable-Migrations -EnableAutomaticMigrations
  PM> Enable-Migrations -EnableAutomaticMigrations
  This will enable automatic migrations for our project. Because we used the "-EnableAutomaticMigrations” switch, it will create our Configuration class with a constructor that sets the AutomaticMigrationsEnabled property set to true:
  1: public Configuration()
  2: {
  3: AutomaticMigrationsEnabled = true;
  4: }
  We’ll now add our initial migration:
  PM> Add-Migration Initial
  PM> Add-Migration Initial
  This will create a migration class call “Initial” that contains the entire model. But we need to remove all of this code because our database already exists so we are just left with empty Up() and Down() methods.
  1: public partial class Initial : DbMigration
  3: public override void Up()
  4: {
  5: }
  6:
  7: public override void Down()
  8: {
  9: }
  10: }
  If we don’t remove this code, we’ll get an exception the first time we attempt to run migrations that tells us: “There is already an object named 'UserProfile' in the database”. This blog post by Julie Lerman fully describes this scenario (i.e., enabling migrations on an existing database).
  Our next step is to add the Entity Framework initializer that will automatically use Migrations to update the database to the latest version. We will add these 2 lines of code to the Application_Start of the Global.asax:
  1: Database.SetInitializer(new MigrateDatabaseToLatestVersion<UsersContext, Configuration>());
  2: new UsersContext().Database.Initialize(false);
  Note the Initialize() call will force the initializer to run if it has not been run before.
  At this point, we can publish again to make sure everything is still working as we are expecting. This time we’re going to specify in our publish profile that Code First Migrations should be executed:
  Once we have re-published we can once again navigate to the Register page. At this point the database has not been changed but Migrations is now enabled on our SQL Database in Azure. We can now customize our model. Let’s add 2 new properties to the UserProfile class – Email and DateOfBirth:
  1: [Table("UserProfile")]
  2: public class UserProfile
  3: {
  4: [Key]
  5: [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
  6: public int UserId { get; set; }
  7: public string UserName { get; set; }
  8: public string Email { get; set; }
  9: public DateTime DateOfBirth { get; set; }
  At this point all we need to do is simply re-publish. We’ll once again navigate to the Registration page and, because we had Automatic Migrations enabled, the database has been altered (*not* recreated) to add our 2 new columns. We can verify this by once again looking at SQL Management Studio:
  Automatic Migrations provide a quick and easy way to keep your database in sync with your model without the worry of having to re-create your entire database and lose data. With Azure Web Sites you can set up automatic deployment with Git or TFS and automate the entire process to make it dead simple.
  Skin design by Mark Wagner, Adapted by David Vidmar