DacDeploy ScriptDatabaseOptions 选项在部署期间被忽略

问题描述 投票:0回答:1

我编写了一个工具来更新多个数据库,作为我们 DEVOPS CI/CD 管道的一部分。这个工具工作没有问题。

我们正在迁移 SaaS 产品的主机平台,作为其中的一部分,我想更改 MS SQL 数据库的恢复模式。同时,我也不想改变旧平台的恢复模式。

DACPAC 文件中的恢复模式设置为 SIMPLE。在新的目标平台上,我希望它是完整的。

据我所知,我应该可以通过设置来做到这一点:

脚本数据库选项= false

这在部署过程中会被忽略。如果我在 Management Studio 中将目标数据库设置为完整恢复模式,则在使用 ScriptDatabaseOptions = false 部署 DACPAC 后,恢复模式将恢复为简单模式。

我知道我可以打开 DACPAC 并更改 RecoveryMode 属性作为 DEVOPS 管道的一部分,但这会很痛苦,因为我们有很多管道和阶段。

如何强制部署正确忽略恢复模式?

using Microsoft.Data.SqlClient;
using Microsoft.SqlServer.Dac;

public void ProcessDacpac(string dacpacFile)
{
    foreach (var sqlConnectionString in sqlConnectionStrings)
    {
        Logger.LogToFile($"Start DacPackage Deploy for: {sqlConnectionString.Key}",Logger.LogCondition.Verbose);

        //check if server and database is online
        Logger.LogToFile($"Testing Connection For: {sqlConnectionString.Key}",Logger.LogCondition.Verbose);

        if (!IsServerAndDatabaseOnline(sqlConnectionString.Value))
        {
            Logger.LogToFile($"Connection Failed: {sqlConnectionString.Key}", Logger.LogCondition.Critical);
            continue;
        }
        else
        {
            Logger.LogToFile($"Testing Connection For: {sqlConnectionString.Key} Success", Logger.LogCondition.Verbose);
        }

        var services = new Microsoft.SqlServer.Dac.DacServices(sqlConnectionString.Value);
        services.Message += (object sender, Microsoft.SqlServer.Dac.DacMessageEventArgs eventArgs) => Logger.LogToFile(eventArgs.Message.Message, Logger.LogCondition.Verbose);

        var dacpac = DacPackage.Load(dacpacFile);
        
        //publish options
        var dacDeployOptions = new DacDeployOptions()
        {
            ScriptDatabaseOptions = false, // <------- this is ignored!
            BlockOnPossibleDataLoss = false, //All these other options are respected. 
            IgnoreColumnOrder = true,
            AllowIncompatiblePlatform = true,
            ExcludeObjectTypes = new ObjectType[] { ObjectType.Users, ObjectType.RoleMembership, ObjectType.Permissions, ObjectType.Logins },
            TreatVerificationErrorsAsWarnings = true
        };

        try
        {
            services.Deploy(dacpac, sqlConnectionString.Key, true, dacDeployOptions);
        }
        catch (Exception ex)
        {
            Logger.LogToFile(ex.Message, ex, Logger.LogCondition.Critical);
            throw;
        }

        Logger.LogToFile($"END DacPackage Deploy for: {sqlConnectionString.Key}", Logger.LogCondition.Verbose);
    }
}
c# sql-server sqlclient dacpac dac
1个回答
0
投票

我将 Microsoft.SqlServer.DacFx 升级到版本 162.4.92,这解决了该问题。我之前使用的是 162.3.566.1 版本。

我只能假设以前的版本存在错误。

© www.soinside.com 2019 - 2024. All rights reserved.