question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run-time connection string updates

See original GitHub issue

Is it possible in v3 to update the connection string at run-time? I am trying to iterate over several databases with different connection strings and apply the same migrations from a .dll to each database. I want to omit the .WithGlobalConnectionString runner builder extension (this won’t be necessarily known at startup) and just set the connection string in a loop.

My service provider is created like this:

static IServiceProvider CreateServices()
{
    return new ServiceCollection()
        .AddFluentMigratorCore()
        .ConfigureRunner(rb => rb
            .AddSqlServer()
            .WithMigrationsIn(typeof(SomeMigrationClass).Assembly))
        .AddLogging(lb => lb.AddFluentMigratorConsole())
        .BuildServiceProvider(false);
}

I am getting at the ProcessorOptions and attempting to set the connection string like this in my loop:

var migrationRunner = _serviceProvider.GetRequiredService<IMigrationRunner>();
var processorOptions = _serviceProvider.GetRequiredService<IOptionsSnapshot<ProcessorOptions>>().Value;

processorOptions.ConnectionString = "MYCONNECTIONSTRING";

migrationRunner.MigrateUp();

When I do this, the migrationRunner.Processor.ConnectionString is never changed. It uses the default “No Connection”. Am I doing something wrong?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
derekgreercommented, May 16, 2018

Perhaps it would be better to pose the desired solution and then ask whether fluent migrator currently supports it.

Here’s what I’d say you want to do:

foreach(var client in clients)
{
    var migrator = _migratorFactory.CreateMigrator(client.ConnectionString);
    migrator.MigrateUp();
}
1reaction
fubar-codercommented, Nov 18, 2020

@nZeus As a migration is something that you should only do at the start of your services, why don’t you just create your own service collection just for the migration services?

I don’t think that there is a need to have long-lived services with changing connection strings.

BTW: Adding SetConnectionString to the migration runner will most likely never happen. As I wrote, you can always create your own service collection and just set the connection string with a .WithGlobalConnectionString(connectionString) in the ConfigureRunner extension method.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to convert runtime connection string to its design- ...
I updated to Visual Studio 2013 last week and I can no longer update my Entity Data model through the visual studio designer...
Read more >
Connection Strings and Configuration Files - ADO.NET
Learn how to store connection strings for ADO.NET applications in an application configuration file, as a best practice for security and ...
Read more >
How to: Save and Edit Connection Strings - Visual Studio ...
To quickly modify the connection string, edit the ConnectionString property, or click the down arrow on the Connection property and choose New ...
Read more >
Can not uncheck "Use this connection string at runtime ...
Under the Remote connection string, there is a checkbox for "Use this connection string at runtime (update destination web.config)" which is ...
Read more >
Entity Framework 6 – update connection string at runtime | tatvog
By default EF will use the connection string in the app.config file, but you can override by using the ConnectionString property as shown ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found