Method migrations entity framework

1

Usually when working with the entity framework, we use the

add-migration migrationName
update-database 

I would like to make this a method, so when you call it, it runs.

Reason: I am implementing multitenancy in my bank, I will separate my clients by schema. Soon the schema will be set and when executing this update, I will create the new schema for that particular client, I can not leave automatic, because if I have N schema, it will not be able to update all the schemas, only what is set as default / current

    
asked by anonymous 12.07.2014 / 02:06

1 answer

1

There is a way to execute migrate.exe manually, in command line .

For example:

Migrate.exe MyApp.exe /startupConfigurationFile="MyApp.exe.config" /targetMigration="update-database"

To create a method that does this, you must create a process and enter the parameters needed to execute Migrate.exe :

using System.Diagnostics;
...
private void ExecuteMigration() 
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = "Migrate.exe";
    startInfo.Arguments = @"MyApp.exe /startupConfigurationFile=\"MyApp.exe.config\" /targetMigration=\"update-database\"";
    Process.Start(startInfo);
}

This way you can run migration through a method within your program.

The Migrate.exe is in <project folder>\packages\EntityFramework.<version>\tools

See some more information about Migrate.exe on the MSDN site

    
16.07.2014 / 19:58