.NET migrations: Setup and migrate multiple databases at runtime

Brief introduction:
I have this ASP.NET Webforms site with the particularity that it doesn’t have only 1 database, it has many.
Why? Because you can create new “instances” of the site on-the-fly. Every “instance” share the same codebase, but has its own database. These all databases have the same schema (structure) but of course different data. Don’t ask ‘why don’t you put everything in one database and use InstanceId to know which is” because it’s a business policy thing.

The application knows which instance is being requested because of the url. There is one extra database to accomplish this (I do know its connection string in design time). This database has only 2 tables and associates urls to ‘application instances’. Then, of course, each ‘application instance’ has its associated connection string.

Current situation: There is nothing being used right now to help us with the job of mantaining every instance database in sync (propagating schema changes to every one). So we are doing it by hand, which of course it’s a total mess.

Question: I’d like to use a rails-migration way to handle schema changes, preferably migratordotnet, but could use any other if it’s easier to setup.

The problem is that migratordotnet needs the connnection string to be declare in the proj.build file and I don’t know them until runtime.

What it would be REALLY useful is some kind of method running on Application_Start that applies the latest migration to every database.

How could this be done with migratordotnet or any similar? Any other suggestion is thanksfully welcomed.

Thank you!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Since this is an old question, I assume that you have solved the problem in some manner or another, but I’ll post a solution anyway for the benefit of other people stumbling across this question. It is possible to invoke MigratorDotNet from code, rather than having it as an MSBuild target:

public static void MigrateToLastVersion(string provider, string connectionString)
{
    var silentLogger = new Logger(false, new ILogWriter[0]);
    var migrator = new Migrator.Migrator(provider, connectionString, 
                   typeof(YourMigrationAssembly).Assembly, false, silentLogger);
    migrator.MigrateToLastVersion();
}

Method 2

RedGate has a SQL Comparison SDK that could be used. Here is a Case Study that looks promising, but I can’t tell you anthing from experience as I haven’t used it. Download the trial and kick the tires.

Method 3

You could use Mig# to maintain your migrations in your C# or .NET code: https://github.com/dradovic/MigSharp

Method 4

Check out Fluent-Migrator.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x