1.21 jigowatts

Great Scott!

Entity Framework~Code-Based Migration - マイグレーションを有効化する

概要

パッケージマネージャーコンソールからEntity Frameworkを操作して、Code Firstマイグレーションを出来るようにします。

環境

Microsoft Visual Studio Community 2013
Entity Framework 6.1.3

実践

1.ヘルプを表示する

Entity Frameworkのコマンドレットを確認しておきましょう。

PM> Get-Help EntityFramework

f:id:sh_yoshida:20150609203453p:plain

2.マイグレーションを有効にする

PM> Enable-Migrations

f:id:sh_yoshida:20150609205513p:plain
プロジェクトにMigrationsフォルダ、その中にConfiguration.csファイルが作成されます。
f:id:sh_yoshida:20150609205532p:plain
■Configuration.cs

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

    internal sealed class Configuration : DbMigrationsConfiguration<ConsoleApplication1.DAL.EFContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(ConsoleApplication1.DAL.EFContext 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 = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

最初に一回だけやればOKです。
試しに再実行してみたら怒られました。
f:id:sh_yoshida:20150609205647p:plain
Forceオプションを付けて実行すると既存のConfiguration.csを上書きします。

PM> Enable-Migrations -Force

f:id:sh_yoshida:20150609210826p:plain

3.マイグレーションのバージョン管理

ひとまず初回のスナップショットとしてスキーマバージョン"Initial"と名前を付けておきます。

PM> Add-Migration Initial

f:id:sh_yoshida:20150610230251p:plain
Migrationフォルダにタイムスタンプのついたクラスファイルが自動生成されます。
f:id:sh_yoshida:20150610230705p:plain
■201506092253526_Initial.cs

namespace ConsoleApplication1.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class Initial : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.People",
                c => new
                    {
                        ID = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 50),
                        PhoneNumber = c.String(maxLength: 50),
                        Address = c.String(maxLength: 50),
                        Job = c.String(maxLength: 25),
                        UpdatedBy = c.String(),
                        UpdateDate = c.DateTime(),
                    })
                .PrimaryKey(t => t.ID);
            
        }
        
        public override void Down()
        {
            DropTable("dbo.People");
        }
    }
}

4.データベースへ反映

PM> Update-Database

f:id:sh_yoshida:20150610230954p:plain
このタイミングでデータベースへのマイグレーションが反映されます。
データベースの__MigrationHistoryテーブルを確認するとスキーマバージョンが登録されていることが確認できました。
f:id:sh_yoshida:20150610232802p:plain

これ以降はモデルを変更したら、③でマイグレーションを追加して、④で反映の繰り返しになります。