0

Let say I have a random code first migration class defined :

public partial class t2 : DbMigration
{
    public override void Up()
    {
        RenameTable(name: "dbo.EntityC", newName: "EntityCs");
        DropTable("dbo.EntityA");
        DropTable("dbo.EntityB");
    }

    public override void Down()
    {
        CreateTable(
            "dbo.EntityB",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    StringOtherProperty = c.String(),
                })
            .PrimaryKey(t => t.Id);

        CreateTable(
            "dbo.EntityA",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    StringProperty = c.String(),
                })
            .PrimaryKey(t => t.Id);

        RenameTable(name: "dbo.EntityCs", newName: "EntityC");
    }
}

How can I execute it, regardless of the current data model. Can I, by code or powershell, force the execution of this migration ?

1 Answer 1

1
+50

A migration always has a connection to the underlying context and model. That is basically the content of the __MigrationHistory Table.

So with the default DbMigrator it is not possible to execute a migraton without a Context.

But you could use Reflection to get the internal “Operations” property from you custom Migration, pass it manually to the MigrationSqlGenerator and execute the Statements by hand.

SqlServerMigrationSqlGenerator gen = new SqlServerMigrationSqlGenerator();
IEnumerable<MigrationOperation> operations;

var migration = new MyMigration();
migration.Up();

var property = typeof(DbMigration)
    .GetProperty("Operations", BindingFlags.Instance | BindingFlags.NonPublic);

operations = property.GetGetMethod(true)
    .Invoke(migration, null) as IEnumerable<MigrationOperation>;

if (operations != null) {
    var statements = gen.Generate(operations, "2012");

    using (var scope = new TransactionScope()) {
        var connection = new SqlConnection("Data Source=.;Initial Catalog=MigrationTest;Integrated Security=True;");
        connection.Open();
        foreach (var item in statements) {
            var command = connection.CreateCommand();
            command.CommandText = item.Sql;
            command.ExecuteNonQuery();
        }
        scope.Complete();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.