1

I have a project that has multiple environments and each environment uses its own database. I've recently switched the database over to EF to make updating it easier. The only catch, I can't seem to get the connection string for EF to transform.

Currently, my process is as follows:

  • I have a SetParameters.xml file with the default connection string for the site.
  • I have a SetParameters.ENV.xml file for each environment with the modified connection strings.
  • I use Bamboo to create a build using msbuild.
  • I use Bamboo to deploy and transform using msdeploy.

The transforms on the regular connection strings work fine.

According to this documentation, the Web.config file will get an additional connection string added to it. In my case, it's Main_DatabasePublish.

I cannot seem to get that to transform. If I add a default connection to the publish profile, it will get set to that string, but unfortunately it still doesn't get transformed to use the proper database.

I'd like to add that I'm not using EF Core. It seems that that would have been easier with the command line.

Any thoughts?

Update:

Thanks to @SteveGreene, I was able to get this working. Turns out what I needed was to add a few lines to my Application_Start method to call the update. I'm not sure if this is the best way, but it does work.

using System.Data.Entity;
using System.Data.Entity.Migrations;

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, MyConfiguration>());
var dbMigrator = new DbMigrator(new MyConfiguration());
dbMigrator.Update();
gin93r
  • 1,551
  • 4
  • 21
  • 39
  • Progress has been made. It now transforms the connection string. Though the database isn't updated on first run like it's supposed to be. – gin93r May 16 '18 at 17:53
  • How are you updating the database? Migrations, initializers or some other method? – Steve Greene May 16 '18 at 17:58
  • I'm using migrations and then there's the `MigrateDatabaseToLatestVersion` initializer that gets added to the `Web.config` file upon deployment. – gin93r May 16 '18 at 18:04
  • 1
    Take a look at these issues, especially the last one where he was changing connection strings and the initialization was occurring before that: https://stackoverflow.com/questions/18550011/migratedatabasetolatestversion-not-executed – Steve Greene May 16 '18 at 18:51
  • Thanks @SteveGreene, that link led me to a solution. Much appreciated. – gin93r May 17 '18 at 12:50
  • You should write up an answer for future users. – Steve Greene May 17 '18 at 20:46
  • @SteveGreene I can do that. I was giving it time in case you wanted to post an answer. :) – gin93r May 22 '18 at 15:00

1 Answers1

1

What I ended up doing was adding the following to Global.asax::Application_Start

using System.Data.Entity;
using System.Data.Entity.Migrations;

System.Data.Entity.Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());
var dbMigrator = new DbMigrator(new Configuration());
var migrations = dbMigrator.GetPendingMigrations();
if (migrations.Any())
{
    dbMigrator.Update();
}
gin93r
  • 1,551
  • 4
  • 21
  • 39