2016-08-12 12 views
0

我發現自己有一點難題,並希望有人在這裏可以提供幫助。MigratorDotNet需要更改數據庫,但不能使用

我繼承了一箇舊項目,它使用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

我不認爲ALTER DATABASE適合遷移。就像開始創建數據庫一樣,DBA應該以交互方式自行完成。 –

回答

0

您必須在更改數據庫之前提交已運行的事務。

相關問題