How to manually (via code) apply DbMigration which was manually crafted?

c# dbmigrate entity-framework-6


I manually created a class

  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");


and i want to apply it via code-first migrations from code like this :

      var migration = new AddClientsTable();

which I stole from here but when I run the code I'm getting this exception :

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

HistoryOperation is the operation which updates __MigrationHistory table ? so How do I do that via code ?

Am I missing something or the EntityFrameowrk Update-Database command does more than what I'm aware of ?

11/28/2016 7:20:20 PM

Popular Answer

It doesn't make sense to cherry pick a migration and run it, because the migrations are cumulative and must be run in sequence. As such, you'd be better to run the equivalent of update-database powershell command at application startup.

Here's some code we use to do that:

In the Configuration.cs class constructor (this file was made when you enable-migrations)

AutomaticMigrationsEnabled = false;
AutomaticMigrationDataLossAllowed = false;

then at app startup call the following method:

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+++++
            var database = databaseContext.Database;
            var migrationConfiguration = dbMgConfig;
            migrationConfiguration.TargetDatabase =
                new DbConnectionInfo(database.Connection.ConnectionString,
            var migrator = new DbMigrator(migrationConfiguration);
        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);
11/28/2016 7:51:50 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow