Skip to content

Filtering

Simon Hughes edited this page Aug 18, 2022 · 10 revisions

This page applies to v3 only. Filtering has changed from the old single Regex you got in version 2. In Version 3, you have much more control, making it simpler to use.

Multi context generation

Multi-context generation does not use FilterSettings at all. Instead, filters are read from a set of database tables which are documented here.

For multi-context filtering (Settings.GenerateSingleDbContext = false), please stop reading this page and go here instead: Generating multiple database contexts in a single go

Single context generation

Gone are the days of a single do-it-all regex; you can now split them up into much smaller Regex. You can have as many as you like and mix and match them.

You now supply your filters via the FilterSettings static class. Your settings are passed to the SingleDbContextFilter to do the actual filtering.

You can override any of the boolean flags in your .tt file with any of the following. Below are the defaults:

FilterSettings.IncludeViews                = true;
FilterSettings.IncludeSynonyms             = false;
FilterSettings.IncludeStoredProcedures     = true;
FilterSettings.IncludeTableValuedFunctions = false; // If true, for EF6 install the EntityFramework.CodeFirstStoreFunctions NuGet Package.

Schema filtering

To include only the schemas 'dbo' and 'events'

FilterSettings.SchemaFilters.Add(new RegexIncludeFilter("^dbo$|^events$"));

You can also edit the SchemaFilter.IsExcluded() and HasNameFilter.IsExcluded() functions to filter using a function instead of a regex.

Table filtering

To include all the customer tables, but not the customer billing tables

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

// This includes any remaining tables with names beginning with 'customer'
FilterSettings.TableFilters.Add(new RegexIncludeFilter("^[Cc]ustomer.*"));

// To exclude all tables that contain '_FR_' or begin with 'data_'
FilterSettings.TableFilters.Add(new RegexExcludeFilter("(.*_FR_.*)|(^data_.*)"));

// To pass in your custom Regex
FilterSettings.TableFilters.Add(new RegexIncludeFilter(new Regex("^tableName1$|^tableName2$", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(200))));

You can also edit the TableFilter.IsExcluded() and HasNameFilter.IsExcluded() functions to filter using a function instead of a regex.

Columns filtering

To exclude any columns that begin with 'FK_'

FilterSettings.ColumnFilters.Add(new RegexExcludeFilter("^FK_.*$"));

You can also edit the ColumnFilter.IsExcluded() and HasNameFilter.IsExcluded() functions to filter using a function instead of a regex.

Stored procedure filtering

To include only the stored procedures that begin with 'Order'

FilterSettings.StoredProcedureFilters.Add(new RegexIncludeFilter("^Order.*$"));

You can also edit the StoredProcedureFilter.IsExcluded() and HasNameFilter.IsExcluded() functions to filter using a function instead of a regex.

Other customisations

Table/column/etc. renaming and other useful customisations are done by editing the callback functions within your <database>.tt file.

Using your functions to filter

Create a new file called Filters.ttinclude. In that file, we will add an example table filter to filter out tables with 'order' in the name. Add the following code to that file (don't miss the + on line 1):

<#+
public class MyTableFilter : IFilterType<Table>
{
    public bool IsExcluded(Table t)
    {
        return t.DbName.ToLowerInvariant().Contains("order");
    }
}
#>

In your <database>.tt settings file, add line 2 so the top of your file looks like this:

<#@ include file="EF.Reverse.POCO.v3.ttinclude" #>
<#@ include file="Filters.ttinclude" #>

In your <database>.tt settings file, add a line below FilterSettings.AddDefaults();

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

Save your <database>.tt settings file.

Clone this wiki locally