-
Notifications
You must be signed in to change notification settings - Fork 226
Custom File‐Based Templates
File-based templates let you store your Mustache templates on disk and customize them independently of the generator version. This is useful when you want full control over the generated code and want changes to persist across generator upgrades.
Set TemplateType to a FileBased variant matching your target EF version:
Settings.TemplateType = TemplateType.FileBasedCore10; // EF Core 10
Settings.TemplateType = TemplateType.FileBasedCore9; // EF Core 9
Settings.TemplateType = TemplateType.FileBasedCore8; // EF Core 8
Settings.TemplateType = TemplateType.FileBasedEf6; // EF 6Also set the GeneratorType to match:
Settings.GeneratorType = GeneratorType.EfCore; // For EfCore 8/9/10
Settings.GeneratorType = GeneratorType.Ef6; // For EF 6// Absolute path
Settings.TemplateFolder = @"c:\path_to_your_templates";
// Relative to the .tt file (recommended)
Settings.TemplateFolder = Path.Combine(Settings.Root, "Templates");Settings.Root is the full directory path where your Database.tt file lives.
The folder you name must contain the .mustache and .txt files directly - it is not a parent holding
per-version sub-folders. Copy the contents of one _File based templates\Templates.* folder into it.
The template folder contains two types of files:
One using namespace per line, without the using keyword or semicolons. There is one per generated artefact
(PocoUsings.txt, PocoConfigurationUsings.txt, DatabaseContextUsings.txt,
DatabaseContextInterfaceUsings.txt, DatabaseContextFactoryUsings.txt, FakeDatabaseContextUsings.txt,
FakeDbSetUsings.txt, StoredProcReturnModelUsings.txt, EnumUsings.txt). Example contents of
PocoUsings.txt:
System
System.Collections.Generic
System.ComponentModel.DataAnnotations
System.ComponentModel.DataAnnotations.Schema
Mustache templates that transform data model objects into generated C# code.
Mustache tips:
- Use
{{#newline}}at line endings to control line breaks - Place
{{#if}}/{{/if}}blocks at the start of a line to avoid extra whitespace - Use
{{#each}}/{{/each}}to iterate over collections
Example — Poco.mustache snippet:
{{ClassComment}}
{{#if HasAttributes}}
{{Attributes}}
{{/if}}
{{ClassModifier}} class {{NameHumanCase}}{{BaseClasses}}{{#newline}}
{{{#newline}}
{{#each Columns}}
{{#if HasAttributes}}
{{Attributes}}
{{/if}}
{{PropertyModifier}} {{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
{{/each}}
}{{#newline}}The latest built-in template files are in the _File based templates/ directory in the repository, one folder
per target: Templates.EF6, Templates.EFCore8, Templates.EFCore9, Templates.EFCore10. Copy the
contents of the folder matching your TemplateType into your own template folder, then customise from
there.
For multi-context generation with file-based templates, see Generating multiple database contexts in a single go.
You can also override the template folder per context:
UPDATE MultiContext.Context SET TemplatePath = 'C:\path\to\templates' WHERE [Name] = 'context_name'File-based templates use Mustache# (a .NET implementation of Mustache). For syntax reference, see the Mustache documentation.
- Settings A-Z - every setting, with a page each
- Common Settings Types Explained
- Settings Callbacks
- Settings runtime values and helpers
- Filtering
- Full Control Over the Generated Code
- Enum Generation from Table Data
- Owned Entities
- JSON column support
- Global Query Filters
- Extended Property Names Feature
- Partial Properties
- File-Scoped Namespaces
- Data Annotations
- Spatial Types
- HierarchyId
- RowVersion and TimeStamp columns
- Lazy Loading
- Stored proc result sets
- Custom File-Based Templates
- Extra entities via partial classes
- INotifyPropertyChanged
- Syntax colour for T4