Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF 6 programmatic Migration with custom connection string

I have an EF6 code first implementation with migrations enabled and a single initial migration. I created a method to programmatically run the migration that looks like the following:

public void RunMigration()
{
    var migrationConfiguration = new DataAccess.Migrations.Configuration();
    var migrator = new System.Data.Entity.Migrations.DbMigrator(migrationConfiguration);
    migrator.Update();
}

When I got to testing I have several configured connection strings in the App.config. My Question:

How do I tell the DbMigrator what connection string to use?

update: I suppose the catch is doing this without extending DbMigrator.

like image 737
AC4 Avatar asked Sep 22 '26 00:09

AC4


2 Answers

I Use

var migrator = new DbMigrator(new DbMigrationsConfiguration { TargetDatabase = new DbConnectionInfo("MyConnectionStringHere")});
migrator.Update();
like image 92
CMS Avatar answered Sep 24 '26 14:09

CMS


So unless someone comes up with a better answer: In EF6 there is no working way as of EF 6.1.3 to specify runtime connection strings for programmatic database migrations. I think the only thing I can see is that I will need to generate an sql script and programmatically run it at time of database setup. The issue with this is that I either have to compile in the text or deploy it with the binary.

like image 27
AC4 Avatar answered Sep 24 '26 15:09

AC4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!