.NET 迁移:在运行时设置和迁移多个数据库 [英] .NET migrations: Setup and migrate multiple databases at runtime

查看:25
本文介绍了.NET 迁移:在运行时设置和迁移多个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:我有这个 ASP.NET Webforms 站点,其特点是它不仅有 1 个数据库,而且有很多.为什么?因为您可以即时创建站点的新实例".每个实例"共享相同的代码库,但有自己的数据库.这些所有数据库都具有相同的架构(结构),但当然数据不同.不要问为什么不将所有内容都放在一个数据库中并使用 InstanceId 知道哪个是",因为这是一个业务策略问题.

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.

由于 url,应用程序知道正在请求哪个实例.有一个额外的数据库来完成这个(我在设计时知道它的连接字符串).该数据库只有 2 个表,并将 url 与应用程序实例"相关联.然后,当然,每个应用程序实例"都有其关联的连接字符串.

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.

问题:我想使用 rails-migration 方法来处理架构更改,最好是 migratordotnet,但如果设置更容易,我可以使用任何其他方法.

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.

问题是 migratordotnet 需要在 proj.build 文件中声明连接字符串,而我直到运行时才知道它们.

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.

真正有用的是在 Application_Start 上运行的某种方法,它将最新的迁移应用到每个数据库.

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

如何使用 migratordotnet 或任何类似工具来完成此操作?欢迎任何其他建议.

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

谢谢!

推荐答案

由于这是一个老问题,我假设你已经以某种方式解决了这个问题,但我还是会发布一个解决方案,以便于其他人偶然发现了这个问题.可以从代码中调用 MigratorDotNet,而不是将其作为 MSBuild 目标:

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();
}

这篇关于.NET 迁移:在运行时设置和迁移多个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆