如何手动(通过代码)应用手动制作的DbMigration?

时间:2016-11-28 19:16:41

标签: c# entity-framework-6 dbmigrate

我手动创建了一个类

  public class AddClientsTable : DbMigration, IMigrationMetadata
  {

    string IMigrationMetadata.Id
    {
      get { return "201611281757258_AddClientsTable"; }
    }

    string IMigrationMetadata.Source
    {
      get { return null; }
    }

    string IMigrationMetadata.Target
    {
      get { return "AddClientsTable-Migration"; }
    }

    public override void Up() {
      CreateTable("Clients", t => new {
          ClientId = t.Guid(name:"ClientId"),
          Name = t.String()
          })
      .PrimaryKey( t => t.ClientId, "ClientId")
      .Index( t => t.ClientId, "PK_Clients", true);
    }

    public override void Down() {
      DropIndex("Clients", "PK_Clients");
      DropTable("Clients");
    }

  }

我希望通过代码首先从代码迁移来应用它:

      var migration = new AddClientsTable();
      migration.Up();
      context.RunMigration(migration);

我从here偷走了,但是当我运行代码时,我得到了这个例外:

Unable to cast object of type 'System.Data.Entity.Migrations.Model.CreateIndexOperation' to type 'System.Data.Entity.Migrations.Model.HistoryOperation'.

HistoryOperation是更新__MigrationHistory表的操作吗?那我怎么通过代码来做呢?

我错过了什么或者EntityFrameowrk Update-Database命令比我所知道的更多吗?

1 个答案:

答案 0 :(得分:0)

挑选迁移并运行它是没有意义的,因为迁移是累积的,必须按顺序运行。因此,您最好在应用程序启动时运行等效的update-database powershell命令。

以下是我们用来执行此操作的一些代码:

Configuration.cs类构造函数中(此文件是在enable-migrations时生成的)

AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;

然后在app启动时调用以下方法:

public static void ApplyDatabaseMigrations()
{
    //Configuration is the class created by Enable-Migrations
    DbMigrationsConfiguration dbMgConfig = new Configuration()
    {
        ContextType = typeof(MyDbContext) //+++++CHANGE ME+++++
    };
    using (var databaseContext = new MyDbContext()) //+++++CHANGE ME+++++
    {
        try
        {
            var database = databaseContext.Database;
            var migrationConfiguration = dbMgConfig;
            migrationConfiguration.TargetDatabase =
                new DbConnectionInfo(database.Connection.ConnectionString,
                                     "System.Data.SqlClient");
            var migrator = new DbMigrator(migrationConfiguration);
            migrator.Update();
        }
        catch (AutomaticDataLossException adle)
        {
            dbMgConfig.AutomaticMigrationDataLossAllowed = true;
            var mg = new DbMigrator(dbMgConfig);
            var scriptor = new MigratorScriptingDecorator(mg);
            string script = scriptor.ScriptUpdate(null, null);
            throw new Exception(adle.Message + " : " + script);
        }
    }
}