Table of Contents

IT:AD:EF/CodeFirst/Migrations:HowTo:Enable-Migrations

Summary

Code Migrations have to be enabled on a project before it can start tracking changes.

Process

In Nuget/Package Manager Console, type:

Enable-Migrations -startupprojectname:App.AppHost.Web -projectname:App.Back.Infrastructure -EnableAutomaticMigrations=false

Note that in a single assembly POC, you could type:

    Enable-Migrations 

    //...which is the same as being verbose regarding EnableAutomaticMigrations:
    //Enable-Migrations -EnableAutomaticMigrations=false    
    

New Migrations Folder

The Enable-Migrations IT:AD:Powershell script creates a new Migrations folder in the IT:AD:Visual Studio project.

New Configuration File

The Migrations folder will contain a single Configuration class file:

namespace XAct.Spikes.CodeMigration.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    //Note: it's sealed, so has to be in same assembly as your app's Dbcontext
    internal sealed class Configuration : DbMigrationsConfiguration<XAct.Spikes.CodeMigration.MyContext>
    {
        public Configuration()
        {
            //Value set by flag when `Enable-Migrations` was invoked:
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(XAct.Spikes.CodeMigration.MyContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  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 = "Andrew Peters" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

New Initial Migration

In addition – depending on the value of -EnableAutomaticMigrations it make another file.

If -EnableAutomaticMigrations is set, no InitialCreate will be generated. But if it is not set, it will also make a new migration (eg, named `201309031323452_InitialCreate'), that will contain a complete description of the db up to that point…

Note:if the db exists, and you invoke it with no flag, it will create the initializer -- but knowing that it will conflict/duplicate the existing db if run: *"Detected database created with a database initializer. Scaffolded migration '201309031323452_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter."*

If you want to start out with no code migrations, and no automated migrations, see this:

 * [[IT/AD/Code First/Migrations/HowTo/Add-Migration/Initial Migration]]

If a First Migration (InitialCreate) is created for you, it will look similar to:

    public partial class InitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.SomeEntities",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.Id);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.SomeEntities");
        }
    }
}

Resources