Skip to content

Filtering

Simon Hughes edited this page Mar 20, 2026 · 13 revisions

Multi-Context Generation

Multi-context generation does not use FilterSettings. See Generating multiple database contexts in a single go.

Single-Context Generation

Filtering for single-context generation is controlled by FilterSettings and the SingleContextFilter class.

In your Database.tt, filtering is set up with:

FilterSettings.Reset();
FilterSettings.AddDefaults();

Then customize as needed below those two lines.

What to Include

FilterSettings.IncludeViews                 = true;   // Include database views
FilterSettings.IncludeSynonyms              = false;  // Include database synonyms
FilterSettings.IncludeStoredProcedures      = true;   // Include stored procedures
FilterSettings.IncludeTableValuedFunctions  = false;  // Include table-valued functions (TVFs)
                                                       // For EF6, install NuGet: EntityFramework.CodeFirstStoreFunctions
FilterSettings.IncludeScalarValuedFunctions = false;  // Include scalar-valued functions

Note: If IncludeTableValuedFunctions or IncludeScalarValuedFunctions is true, IncludeStoredProcedures is automatically set to true.

Schema Filtering

Control which database schemas are included or excluded:

// Include only the 'dbo' and 'events' schemas
FilterSettings.SchemaFilters.Add(new RegexIncludeFilter("^dbo$|^events$"));

// Exclude the 'Finance' schema
FilterSettings.SchemaFilters.Add(new RegexExcludeFilter("[Ff]inance.*"));

Table Filtering

Control which tables (and views) are included or excluded:

// Exclude tables with 'billing' anywhere in the name
FilterSettings.TableFilters.Add(new RegexExcludeFilter(".*[Bb]illing.*"));

// Include only tables whose names begin with 'Customer'
FilterSettings.TableFilters.Add(new RegexIncludeFilter("^[Cc]ustomer.*"));

// Exclude tables matching multiple patterns
FilterSettings.TableFilters.Add(new RegexExcludeFilter("(.*_FR_.*)|(^data_.*)"));

// Pass in a custom compiled Regex with options
FilterSettings.TableFilters.Add(new RegexIncludeFilter(
    new Regex("^tableName1$|^tableName2$", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(200))
));

// Exclude ASP.NET Identity tables (example from default Database.tt)
FilterSettings.TableFilters.Add(new RegexExcludeFilter("AspNet.*"));
FilterSettings.TableFilters.Add(new RegexExcludeFilter("__EFMigrationsHistory"));

Column Filtering

Control which columns are included or excluded:

// Exclude columns whose names begin with 'FK_'
FilterSettings.ColumnFilters.Add(new RegexExcludeFilter("^FK_.*$"));

// Exclude audit columns
FilterSettings.ColumnFilters.Add(new RegexExcludeFilter("[Cc]reated[Aa]t.*"));

Stored Procedure Filtering

Control which stored procedures are included or excluded:

// Include only stored procedures starting with 'Pricing'
FilterSettings.StoredProcedureFilters.Add(new RegexIncludeFilter("Pricing.*"));

// Exclude stored procedures with 'Calc' in the name
FilterSettings.StoredProcedureFilters.Add(new RegexExcludeFilter("Calc"));

Filter Ordering

Filters run in the order they are added. You can chain include and exclude filters:

// First exclude billing tables, then include only customer tables from the remainder
FilterSettings.TableFilters.Add(new RegexExcludeFilter(".*[Bb]illing.*"));
FilterSettings.TableFilters.Add(new RegexIncludeFilter("^[Cc]ustomer.*"));

Custom Function-Based Filters

For more complex filtering logic, create a custom filter class. Add a Filters.ttinclude file alongside your Database.tt:

// Filters.ttinclude
<#+
public class MyTableFilter : IFilterType<Table>
{
    public bool IsExcluded(Table t)
    {
        // Exclude any table whose name contains "order" (case-insensitive)
        return t.DbName.ToLowerInvariant().Contains("order");
    }
}
#>

Include it at the top of Database.tt (after the EF.Reverse.POCO.v3.ttinclude include):

<#@ include file="Filters.ttinclude" #>

Then register the filter:

FilterSettings.TableFilters.Add(new MyTableFilter());

You can create custom filters for Schema, Table, Column, and StoredProcedure types.

Filter Types Reference

Class Description
RegexIncludeFilter Includes items whose name matches the regex
RegexExcludeFilter Excludes items whose name matches the regex
PeriodFilter Excludes items whose name contains a period (EF does not support these)
SchemaFilter Extend this class to add custom schema filtering logic
TableFilter Extend this class to add custom table filtering logic
ColumnFilter Extend this class to add custom column filtering logic
StoredProcedureFilter Extend this class to add custom stored procedure filtering logic
HasNameFilter Filters items based on a name list

Clone this wiki locally