我发现自己有点难题,希望有人在这里帮忙。
我继承了一个旧项目,它使用MigratorDotNet来部署数据库。在测试项目的部署(我们现在使用Octopus作为部署系统)时,我开始收到错误,说明代理未启用,导致应用程序失败。我创建了一个迁移以在数据库上启用代理,但是我为它创建的迁移返回一个错误,指出“在多语句事务中不允许使用ALTER DATABASE语句”。
以下是迁移:
[CLSCompliant(false)]
[Migration(201608121015)]
public class EnableSqlServiceBroker : Migration
{
public override void Down()
{
var assemblyRoot = System.Reflection.Assembly.GetExecutingAssembly().Location;
var webConfigFileLocation = Path.Combine(assemblyRoot.Substring(0, assemblyRoot.IndexOf("\\bin")), "Web.config");
var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = webConfigFileLocation }, ConfigurationUserLevel.None);
var appSettings = (configuration.GetSection("appSettings") as AppSettingsSection);
var databaseName = appSettings.Settings[Constants.DatabaseName].Value;
var stringBuilder = new StringBuilder()
// Declare the BrokerEnabled variable and set it
.AppendLine("DECLARE @BrokerEnabled bit")
.AppendLine("SET @BrokerEnabled = 0")
.AppendLine(
string.Format("SELECT @BrokerEnabled = is_broker_enabled FROM sys.databases WHERE name = '{0}'",
databaseName))
.AppendLine("IF(@BrokerEnabled = 1)")
.AppendLine("BEGIN")
// disable the broker
.AppendLine(string.Format(" ALTER DATABASE [{0}] SET DISABLE_BROKER", databaseName))
.AppendLine("END");
Database.ExecuteNonQuery(stringBuilder.ToString());
}
public override void Up()
{
var assemblyRoot = System.Reflection.Assembly.GetExecutingAssembly().Location;
var webConfigFileLocation = Path.Combine(assemblyRoot.Substring(0, assemblyRoot.IndexOf("\\bin")), "Web.config");
var configuration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap { ExeConfigFilename = webConfigFileLocation }, ConfigurationUserLevel.None);
var appSettings = (configuration.GetSection("appSettings") as AppSettingsSection);
var databaseName = appSettings.Settings[Constants.DatabaseName].Value;
var stringBuilder = new StringBuilder()
// Declare the BrokerEnabled variable and set it
.AppendLine("DECLARE @BrokerEnabled bit")
.AppendLine("SET @BrokerEnabled = 0")
.AppendLine(string.Format("SELECT @BrokerEnabled = is_broker_enabled FROM sys.databases WHERE name = '{0}'", databaseName))
.AppendLine("IF(@BrokerEnabled = 0)")
.AppendLine("BEGIN")
// enable the broker
.AppendLine(string.Format(" ALTER DATABASE [{0}] SET ENABLE_BROKER", databaseName))
.AppendLine("END");
Database.ExecuteNonQuery(stringBuilder.ToString());
}
}
我无法在不更改数据库的情况下启用代理,但我无法更改数据库,因为这显然是不允许的。有没有人知道解决这个问题的方法?或者修复它?提前谢谢。
答案 0 :(得分:0)
您必须在更改数据库之前提交已在运行的事务。