开发者

EntityFramework.Migrations, using DbMigrator in a C# class

I just installed the new EntityFramework.Migrations package. I scaffoled my migrations following this tutorial: http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-no-magic-walkthrough.aspx

Using the Powershell window, everything works fine.

But we need to create a class that will rollback all the migrations for our automated tests.

So I made a simple class that looks like this:

public class CustomMigrator
{
    public void DropDatabase()
    {
        new DbMigrator(new Settings()).Update("0");
    }

    public void RegenerateDatabase()
    {
        new DbMigrat开发者_开发问答or(new Settings()).Update();
    }
}

Settings is my DbMigrationContext implementation that looks like this:

public class Settings : DbMigrationContext<MyDb>
{
    public Settings()
    {
        AutomaticMigrationsEnabled = false;
        SetCodeGenerator<CSharpMigrationCodeGenerator>();
        AddSqlGenerator<SqlConnection, SqlServerMigrationSqlGenerator>();
    }
}

When I call this:

new CustomMigrator().DropDatabase();

I get a weird exception:

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

I know that Migrations are still in alpha, but I was wondering if anyone have been able to run the migrations using DbMigrator?

Thanks.


I just found my problem, it is because I was using EntityFrameworkProfiler and there is a bug with the latest EF release that breaks the profiler.

http://blogs.hibernatingrhinos.com/5121/entity-framework-june-2011-ctp-v4-2-is-now-supported-in-entity-framework-profiler

For the moment I did not need the profiler, so I just removed the line of code that was initializing the profiler and now it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜