All Articles

EF Code-Fist Migrations: How to set the Migrations folder

In my previous post, I confessed that I am currently using Code-First Migrations. If you are using Code-First migrations in your project, at some point in time you've had to enable Migrations, running on the Package Manager (PM) Console the Enable-Migrations command

Enable-Migrations

Are you curious what this command does? Basically, it looks for the current project to find a DbContext, and generates a new class with the name Configuration, which is placed under the folder with the name Migrations in your root, with the following code

namespace Myapp
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(MyApp.Data.MyDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet.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 = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

Every time you want to add a new migration to your project, you have to run in PM console the Add-Migration command

Add-Migration AddColumnFirstName

Here, AddColumnFistName it's the Migration name, and is just an example of a migration.

After this command, you will have a new class 201311291316579_AddColumnFistName added to you project, which is placed under the folder with the name Migrations. The name of the class is generated following the pattern <current_date_time>_<migration_name>.

Below it's an example of the code generated for the migration

namespace MyApp.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class AddColumnFirstName : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.Person", "FirstName", c =&gt; c.String());
        }
        
        public override void Down()
        {
            DropColumn("dbo.Person", "FirstName");
        }
    }
}

I don't like the location of the Migrations folder, which is being placed on the root of my project. Fortunately, you can set this location. For example, I want to place my Migrations in the Data folder. To do this, when enabling migrations, I have to pass the MigrationsDirectory parameter to the Enable-Migrations command

Enable-Migrations -MigrationsDirectory Data\Migrations

Besides of the new location of the Configuration class, there is one more difference in the generated code, in the Configuration constructor

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    MigrationsDirectory = @"Data\Migrations";
}

From now on, every time I add a new migration, this will be added to the Data\Migrations folder. I like to have a clean root project folder.

 

Just one note: if you already have the Migrations folder in your root, and you wish to have a new location, I suggest to do it manually, moving all the content of the current migrations folder to the new location. Do not foget to change the Configuration constructor as well, setting the MigrationsDirectory property with the new location.