Skip to content

Filtering

Simon Hughes edited this page Aug 30, 2026 · 16 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.

What IncludeViews actually changes

Against a schema containing one view, dbo.ActiveStudent, these are the types generated. Only the type declarations are shown.

FilterSettings.IncludeViews = true

public interface IMyDbContext : IDisposable
public class MyDbContext : DbContext, IMyDbContext
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
public class ActiveStudent
public class Course
public class Document
public class OrderLineItem
public class Student
public class StudentCourse
public class ActiveStudentConfiguration : IEntityTypeConfiguration<ActiveStudent>
public class CourseConfiguration : IEntityTypeConfiguration<Course>
public class DocumentConfiguration : IEntityTypeConfiguration<Document>
public class OrderLineItemConfiguration : IEntityTypeConfiguration<OrderLineItem>
public class StudentConfiguration : IEntityTypeConfiguration<Student>
public class StudentCourseConfiguration : IEntityTypeConfiguration<StudentCourse>
public class GetCourseReportReturnModel
public class ResultSetModel1
public class ResultSetModel2
public class GetStudentsByCourseReturnModel
public class sales_GetOrderTotalsReturnModel

FilterSettings.IncludeViews = false

public interface IMyDbContext : IDisposable
public class MyDbContext : DbContext, IMyDbContext
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
public class Course
public class Document
public class OrderLineItem
public class Student
public class StudentCourse
public class CourseConfiguration : IEntityTypeConfiguration<Course>
public class DocumentConfiguration : IEntityTypeConfiguration<Document>
public class OrderLineItemConfiguration : IEntityTypeConfiguration<OrderLineItem>
public class StudentConfiguration : IEntityTypeConfiguration<Student>
public class StudentCourseConfiguration : IEntityTypeConfiguration<StudentCourse>
public class GetCourseReportReturnModel
public class ResultSetModel1
public class ResultSetModel2
public class GetStudentsByCourseReturnModel
public class sales_GetOrderTotalsReturnModel

A view generates an entity and a configuration class like any table. Because a view has no declared primary key, use Settings.ViewProcessing to say which columns identify a row - without it, every non-nullable column becomes part of a composite key.

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"));

How the filters combine

This is the part that surprises people, so it is worth stating precisely.

An item is excluded if any filter in its list excludes it. The filters are not applied in sequence to a shrinking set - the whole list is evaluated against each item and the answers are OR'd together. Adding filters can therefore only ever remove more; the order you add them in makes no difference to the result.

Multiple RegexIncludeFilters of the same kind are merged into one. Before the first item is tested, every RegexIncludeFilter in a list is combined into a single filter whose pattern is the alternation of theirs (pattern1|pattern2|...). Without that merge, two include filters would exclude everything, because nothing matches both. With it, an item survives if it matches any include pattern.

So this pair:

FilterSettings.TableFilters.Add(new RegexExcludeFilter(".*[Bb]illing.*"));
FilterSettings.TableFilters.Add(new RegexIncludeFilter("^[Cc]ustomer.*"));

keeps the tables that match ^[Cc]ustomer.* and do not match .*[Bb]illing.*. An exclude filter always wins over an include filter, whichever was added first.

The SchemaFilters list is applied to tables and stored procedures too, via their schema. Excluding a schema excludes everything in it, without needing a table filter as well.

The MultiContext schema is always excluded. SchemaFilter, which AddDefaults() installs, drops it because the generator reserves it for multi-context generation. A schema of your own with that name will not be generated.

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.v4.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

All of them implement IFilterType<T>, whose single method bool IsExcluded(T item) returns true to drop the item. Every filter matches against the database name (DbName), not the generated C# name.

Class Description
RegexIncludeFilter Excludes items whose name does not match the regex. Several of these in one list are merged into a single alternation - see above
RegexExcludeFilter Excludes items whose name matches the regex
PeriodFilter Excludes items whose name contains a period (EF does not support these). Installed by default on schemas and stored procedures
SchemaFilter Stub class installed by AddDefaults(). Already excludes the reserved MultiContext schema
TableFilter Stub class installed by AddDefaults()
ColumnFilter Stub class installed by AddDefaults()
StoredProcedureFilter Stub class installed by AddDefaults()
HasNameFilter Stub class installed by AddDefaults(), constructed with a FilterType (Schema, Table, Column, StoredProcedure) so one implementation can serve all four lists

The five stub classes live inside EF.Reverse.POCO.v4.ttinclude, which is regenerated on every upgrade, so do not edit them in place - your changes will be lost. Write your own class implementing IFilterType<T> in a Filters.ttinclude as shown above, and add it to the relevant list.

Clone this wiki locally