Migrations Entity framework method

Asked

Viewed 131 times

1

Usually when working with the Entity framework, we use the calls in the console

add-migration migrationName
update-database 

I’d like to make it a method, so when you call him, he’ll be executed.

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’t leave it automatic, because if I have N schema, it won’t be able to update all schemas, just whatever is set as default/current

  • You want to call a migration at system run time?

  • Exactly that, to create the schema as I mentioned there in the motif, rsrs

  • I see no need. You can simulate the behavior of a Migration through a Action. Stay more within Application Design.

1 answer

1

Is there a way to execute the migrate.exe manually, in command line.

For example:

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

To create a method that performs this, you must create a process and inform the necessary parameters to execute the 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);
}

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

The Migrate.exe gets into <project folder>\packages\EntityFramework.<version>\tools

See more information about Migrate.exe on the website of MSDN

Browser other questions tagged

You are not signed in. Login or sign up in order to post.