xSQL Schema Compare SDK version 11 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. 
             * 
             */
            // create the view filter
            viewFilter = new EntityFilter(SqlEntityTypeEnum.View);
            // create a group for the filter conditions
            group = new EntityFilterGroup(viewFilter);                        
            group.ConditionOperator = EntityFilterOperatorEnum.OR;
            viewFilter.Groups.Add(group);
            // add conditions
            group.Conditions.Add(new EntityNameFilterCondition("temp", EntityFilterConditionTypeEnum.StartingWith, group));
            group.Conditions.Add(new EntityNameFilterCondition("tmp", EntityFilterConditionTypeEnum.EndingWith, group));
                        
            // register the filter
            filterManager.Filters.Add(viewFilter);

            /*
             * Create a procedure filter that excludes all databases procedures, except for spSales and spEmployees.
             *  - The group operator is AND
             *  - The condition used is DifferentFrom
             *  
             * It is important to remember that a filter will exclude objects that match the filter conditions, 
             * so to include spSales and spEmployees, we use the DifferentFrom criteria.
             *  
             */
            // create the procedure filter
            procedureFilter = new EntityFilter(SqlEntityTypeEnum.StoredProcedure);
            // create a group for the filter conditions
            group = new EntityFilterGroup(procedureFilter);
            group.ConditionOperator = EntityFilterOperatorEnum.And;
            procedureFilter.Groups.Add(group);
            // add conditions
            group.Conditions.Add(new EntityNameFilterCondition("spSales", EntityFilterConditionTypeEnum.DifferentFrom, group));
            group.Conditions.Add(new EntityNameFilterCondition("spEmployees", EntityFilterConditionTypeEnum.DifferentFrom, group));            
            // register the filter
            filterManager.Filters.Add(procedureFilter);

            /*
             * Create a function filter that contains two conditions:
             * - 1st condition excludes user-defined functions that belong to the schema Sales
             * - 2nd condition excludes 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.
             * 
             */
            // create the function filter
            functionFilter = new EntityFilter(SqlEntityTypeEnum.UserDefinedFunction);
            
            // create a group for the filter conditions
            group = new EntityFilterGroup(functionFilter);
            group.ConditionOperator = EntityFilterOperatorEnum.OR;
            functionFilter.Groups.Add(group);
            // add conditions
            group.Conditions.Add(new EntitySchemaFilterCondition("Sales", EntityFilterConditionTypeEnum.EqualsTo, group));
            group.Conditions.Add(new EntitySchemaFilterCondition("HumanResources", EntityFilterConditionTypeEnum.EqualsTo, group));            
            // register the filter
            filterManager.Filters.Add(functionFilter);
        

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