在迁移之前,有没有办法将现有数据库置于单用户模式?
我想做类似以下的事情,虽然这不起作用:
var connectionString = "";// Connection string pointing to MASTER database on server
var sqlConnection = new SqlConnection();
sqlConnection.Open();
ExecuteNonQuery(
sqlConnection,
"USE MASTER");
ExecuteNonQuery(
sqlConnection,
$"ALTER DATABASE [TargetDatabaseToMigrate] SET AUTO_UPDATE_STATISTICS_ASYNC OFF");
ExecuteNonQuery(
sqlConnection,
$"ALTER DATABASE [TargetDatabaseToMigrate] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
TargetDatabaseContext ctx = new TargetDatabaseContext(sqlConnection, false);
if(!ctx.Database.CompatibleWithModel(true))
{
var migrator = new MigrateDatabaseToLatestVersion<TargetDatabaseContext, Migrations.Configuration>(true);
migrator.InitializeDatabase(ctx);
}
ExecuteNonQuery(
sqlConnection,
$"ALTER DATABASE [TargetDatabaseToMigrate] SET MULTI_USER");
ExecuteNonQuery(
sqlConnection,
$"ALTER DATABASE [TargetDatabaseToMigrate] SET AUTO_UPDATE_STATISTICS_ASYNC ON");
我查看了EF代码,似乎没有代码路径使用传入的现有连接。所有代码路径似乎都使用连接信息来创建新连接。
例如,这是我的代码在EF中首次失败的地方:
internal override void EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
{
var databaseCreated = false;
var databaseCreator = new DatabaseCreator(_configuration.CommandTimeout);
{
DbConnection connection = null;
try
{
connection = CreateConnection();
除了:
throw new InvalidOperationException(Strings.SqlProvider_CredentialsMissingForMasterConnection, e);
有没有办法实现这个目标?谢谢。