EF 6 programmatic Migration with custom connection string

.net .net-4.5 c# entity-framework entity-framework-6

Question

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.

1
2
12/16/2015 4:49:10 PM

Accepted Answer

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.

2
1/11/2016 8:06:36 PM

Popular Answer

I Use

var migrator = new DbMigrator(new DbMigrationsConfiguration { TargetDatabase = new DbConnectionInfo("MyConnectionStringHere")});
migrator.Update();


Related Questions





Related

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