ここでは、これを使用するサンプルの移行について説明します。 downメソッドでは、既存の "DropForeignKey(...)"メソッドを使用することができます。
using System;
using System.Collections.Generic;
using System.Text;
using SubSonic;
namespace MyNameSpace.Migrations
{
public class Migration001 : Migration
{
public override void Up()
{
TableSchema.Table parent = GetTable("parent");
TableSchema.Table child = GetTable("child");
CreateForeignKeyMySQL(parent.GetColumn("id"), child.GetColumn("parent_id"),
CreateForeignKeyAction.SetNull, CreateForeignKeyAction.Restrict);
base.Up();
}
public override void Down()
{
DropForeignKey(parent.GetColumn("id"), child.GetColumn("parent_id"));
base.Down();
}
#region foreign key helper function
public enum CreateForeignKeyAction
{
Cascade,
Restrict,
SetNull,
NoAction
}
private String CreateForeignKeyActionValue(CreateForeignKeyAction action)
{
switch (action)
{
case CreateForeignKeyAction.Cascade:
return "CASCADE";
case CreateForeignKeyAction.Restrict:
return "RESTRICT";
case CreateForeignKeyAction.SetNull:
return "SET NULL";
case CreateForeignKeyAction.NoAction:
return "NO ACTION";
default:
return "CASCADE";
}
}
public void CreateForeignKeyMySQL(
TableSchema.TableColumn oneTable, TableSchema.TableColumn manyTable,
CreateForeignKeyAction onDelete, CreateForeignKeyAction onUpdate)
{
String sqlAppend = String.Format(" ON DELETE {0} ON UPDATE {1}",
CreateForeignKeyActionValue(onDelete), CreateForeignKeyActionValue(onUpdate));
SubSonic.MySqlGenerator generator = new SubSonic.MySqlGenerator(null);
String sqlCommand =
System.Text.RegularExpressions.Regex.Replace(
generator.BuildForeignKeyStatement(oneTable, manyTable), ";?$", sqlAppend
);
Execute(sqlCommand);
}
#endregion
}
}