xSQL Schema Compare SDK version 9.5 for SQL Server
Entity Filter

Entity Filters, introduced in the version 5 of xSQL Schema Compare SDK, provide a way of including or excluding database objects based on some selection criteria. They replace the previous schema filters, which have been depreciated.

The following code demonstrate some typical entity filters, such as:

The code is based on the AdventureWorks database.

EntityFilterManager filterManager;
EntityFilter viewFilter;
EntityFilter procedureFilter;
EntityFilter functionFilter;
EntityFilterGroup group;


            // create the filter manager; set the exclusion type to ExcludeAlways
            filterManager = new EntityFilterManager(EntityFilterExclusionTypeEnum.ExcludeAlways);
            /*
             * Create a view filter that contains two conditions:
             *  - 1st condition excludes views the name of which start with tmp
             *  - 2nd condition excludes views the name of which end with temp
             *
             * Conditions are applied to the view name, therefore they are created as EntityNameFilterCondition objects.
             *
             */
            viewFilter = new EntityFilter(SqlEntityTypeEnum.View);
           
            // create a new group
            group = new EntityFilterGroup(viewFilter);                       
            // combine the conditions in the group with the OR operator
            group.ConditionOperator = EntityFilterOperatorEnum.OR;
            // the group should EXCLUDE the views that match the conditions
            group.IncludeMatches = false;
           
            // add the group conditions
            group.Conditions.Add(new EntityNameFilterCondition("temp", EntityFilterConditionTypeEnum.StartingWith, group));
            group.Conditions.Add(new EntityNameFilterCondition("tmp", EntityFilterConditionTypeEnum.EndingWith, group));
            viewFilter.Groups.Add(group);
           
            // register the filter
            filterManager.Filters.Add(viewFilter);


            // create a procedure filter
            procedureFilter = new EntityFilter(SqlEntityTypeEnum.StoredProcedure);
            group = new EntityFilterGroup(procedureFilter);
            group.IncludeMatches = false;
            // add the condition that excludes procedures the name of which starts with tmp.
            group.Conditions.Add(new EntityNameFilterCondition("tmp", EntityFilterConditionTypeEnum.StartingWith, group));
            procedureFilter.Groups.Add(group);
            filterManager.Filters.Add(procedureFilter);


            /*
             * Create a function filter that contains two conditions:
             * - 1st condition includes the user-defined functions that belong to the schema Sales
             * - 2nd condition includes the user-defined functions that belong to the schema HumanResources.
             *
             * Conditions in this case are applied to the schema, so they are created as EntitySchemaFilterCondition objects.
             * Since a function can satisfy either one of the conditions, the group filter combines them with the OR operator.
             *
             * It is important to remember that the filter will exclude all other database functions that do not meet the filter's criteria. The only functions that will be compared and synchronized
             * are the ones that belong to the schema Sales or HumanResources.
             *
             */
            functionFilter = new EntityFilter(SqlEntityTypeEnum.UserDefinedFunction);
           
            // create a new group
            group = new EntityFilterGroup(functionFilter);
            // combine the conditions in the group with the OR operator
            group.ConditionOperator = EntityFilterOperatorEnum.OR;
           
            // add the group conditions; both conditions require an exact match of the schema, so the EqualsTo condition type is used
            group.Conditions.Add(new EntitySchemaFilterCondition("Sales", EntityFilterConditionTypeEnum.EqualsTo, group));
            group.Conditions.Add(new EntitySchemaFilterCondition("HumanResources", EntityFilterConditionTypeEnum.EqualsTo, group));
            functionFilter.Groups.Add(group);
            // register the filter
            filterManager.Filters.Add(functionFilter);

            // set the schema compare filter property
            comparer.EntityFilterManager = filterManager;