在SSDT模式中比较如何忽略“模式”类型对象的差异

时间:2013-07-17 20:32:40

标签: sql-server schema sql-server-2012 database-project sql-server-data-tools

从Schema Compare Options中,我取消选择所有对象类型:

Schema compare object type options

它仍然显示我在Schema对象中的差异:

Difference table showing schema objects

我滚动浏览了常规选项的大列表,但似乎没有人这样做:

General options

3 个答案:

答案 0 :(得分:2)

我砍了它。如果保存比较,可以将其添加到文件中:

  <PropertyElementName>
    <Name>Microsoft.Data.Tools.Schema.Sql.SchemaModel.SqlSchema</Name>
    <Value>ExcludedType</Value>
  </PropertyElementName>

你会看到打开它的位置。此设置不在UI中,但显然支持。

答案 1 :(得分:0)

右键单击顶级节点(添加,更改,删除),您可以选择“全部排除”以取消选中该类型的所有元素。这至少可以让你快速进入一个未经检查的状态。

答案 2 :(得分:0)

您可以通过在执行模式合并之前以exe身份运行以下内容来在代码中设置排除模式。下面的代码需要将Microsoft.SqlServer.DacFx nuget包添加到您的项目中。它包含2个参数,一个是.scmp文件路径,第二个是逗号分隔的模式字符串以排除。它将覆盖提供的.scmp并排除您提供的架构名称。

实际上,它在.scmp文件中添加了XML部分,这等效于取消选中UI上的对象并保存该文件。 (坚持的偏好)

如果要在部署期间不合并一个架构,则此exe执行可能是VSTS(VSO)发布管道中的任务。

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.SqlServer.Dac.Compare;

namespace DatabaseSchemaMergeHelper
{
    /// <summary>
    /// Iterates through a supplied schema compare file and excludes objects belonging to a supplied list of schema
    /// </summary>
    class Program
    {
        /// <summary>
        /// first argument is the scmp file to update, second argument is comma separated list of schemas to exclude
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            if (args.Length == 0) return;

            var scmpFilePath = args[0];
            var listOfSchemasToExclude = args[1].Split(',').ToList();

            // load comparison from Schema Compare (.scmp) file
            var comparison = new SchemaComparison(scmpFilePath);
            var comparisonResult = comparison.Compare();

            // find changes pertaining to objects belonging to the supplied schema exclusion list
            var listOfDifferencesToExclude = new List<SchemaDifference>();

            // add those objects to a list
            foreach (SchemaDifference difference in comparisonResult.Differences)
            {
                if (difference.TargetObject != null &&
                    difference.TargetObject.Name != null &&
                    difference.TargetObject.Name.HasName &&
                    listOfSchemasToExclude.Contains(difference.TargetObject.Name.Parts[0], StringComparer.OrdinalIgnoreCase))
                {
                    listOfDifferencesToExclude.Add(difference);
                }
            }

            // add the needed exclusions to the .scmp file
            foreach (var diff in listOfDifferencesToExclude)
            {
                if (diff.SourceObject != null)
                {
                    var SourceExclusionObject = new SchemaComparisonExcludedObjectId(diff.SourceObject.ObjectType, diff.SourceObject.Name,
                                                                                     diff.Parent?.SourceObject.ObjectType, diff.Parent?.SourceObject.Name);
                    comparison.ExcludedSourceObjects.Add(SourceExclusionObject);
                }

                var TargetExclusionObject = new SchemaComparisonExcludedObjectId(diff.TargetObject.ObjectType, diff.TargetObject.Name,
                                                                                 diff.Parent?.TargetObject.ObjectType, diff.Parent?.TargetObject.Name);
                comparison.ExcludedTargetObjects.Add(TargetExclusionObject);
            }

            // save the file, overwrites the existing scmp.
            comparison.SaveToFile(scmpFilePath, true);
        }
    }
}