1

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"); DropTable("Clients"); } } 

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

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

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 ?

5
  • why exactly are you trying to do it by code again? Commented Nov 28, 2016 at 19:19
  • well, I don't use visual studio ( too slow on my very old machine ) to execute Update-Database command, I just use gvim to do code editing and I really want to be able to do Code-First Databse Migration just like that. Commented Nov 28, 2016 at 19:23
  • Do you have powershell? There are powershell commands for each of the database functions for entity-framework Commented Nov 28, 2016 at 19:31
  • yes, I'm on windows and I do have powershell, but the need for doing it in code comes from the fact that I need to apply same migrations on my users' machines so it'll be easier to migrate their local databases. Commented Nov 28, 2016 at 19:37
  • If you want to execute all new migrations using code first, try MigrateDatabaseToLatestVersion msdn.microsoft.com/en-us/library/… (sample here github.com/tym32167/arma3beclient/blob/master/src/…) Commented Nov 28, 2016 at 19:39

1 Answer 1

1

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+++++ { 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); } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Tried your solution but was faced with another problem, ` string IMigrationMetadata.Target { get { return "AddClientsTable-Migration"; } }` this field needs to be a Base64 of the model (?), how can I generate it from code or from powershell ? I'm thinking of using powershell from comand line to generate the migrations and applying them using yoyr snipppet of code.
@Abdelghani This is what the Add-Migration command is for. Good luck navigating migrations without it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.