Skip to content

Custom file based templates

Simon Hughes edited this page Feb 16, 2023 · 13 revisions

This is activated if

Settings.TemplateType = TemplateType.FileBased;

Using Settings.TemplateType = TemplateType.FileBased means that you want to use your own custom templates for file generation. Therefore you need to specify the folder where those templates exist.

Single context generation

If you are using the single context generation (Settings.GenerateSingleDbContext = true), you can do that via:

Settings.TemplateFolder = "c:\\path_to_your_templates"; // Specify your path here

If the path is relative to your project, you can use:

Settings.TemplateFolder = Path.Combine(Settings.Root, "Templates");

Settings.Root is set to the full path where your <database.tt> file exists.

Templates

The latest template files can be found here.

Multi-context generation

If you are using the multi-context generation (Settings.GenerateSingleDbContext = false), then please read this.

Required template files

The templates use a mixture of plain text files for a list of Usings to place at the top of the generated code, and a mustache template file. The {{Mustache}} template documentation is available at github.com/jehugaleahsa/mustache-sharp

Each template is passed in a data model to convert into code. This model is created by the CodeGenerator class which is then passed, along with the template text, to the mustache compiler to be transformed.

Thanks to the awesome work by Travis Parks and Keith Williams for the Mustache# for .NET Core library which is available at github.com/SunBrandingSolutions/mustache-sharp

Template types

These fall into two types:

  • txt files - These contain the usings required for the template. Place each using namespace on a separate line, no using or semi-colon ;. For example:
    System
    System.Data
    System.Linq
    
  • mustache files - These contain the mustache template text used to convert the passed-in model into code.

Examples

Not all examples are listed here, just a few of the smallest ones to give you an idea of what they look like.

Enums.mustache

{{#each EnumAttributes}}
{{this}}{{#newline}}
{{/each}}
public enum {{EnumName}}{{#newline}}
{{{#newline}}
{{#each Items}}
{{#each Attributes}}
    {{this}}{{#newline}}
{{/each}}
    {{Key}} = {{Value}},{{#newline}}
{{/each}}
}{{#newline}}

StoredProcReturnModels.mustache

{{ResultClassModifiers}} class {{WriteStoredProcReturnModelName}}{{#newline}}
{{{#newline}}
{{#if SingleModel}}
{{#each SingleModelReturnColumns}}
    {{this}}{{#newline}}
{{/each}}
{{#else}}
{{#each MultipleModelReturnColumns}}
    public class ResultSetModel{{Model}}{{#newline}}
    {{{#newline}}
{{#each ReturnColumns}}
        {{this}}{{#newline}}
{{/each}}
    }{{#newline}}
    public List<ResultSetModel{{Model}}> ResultSet{{Model}};{{#newline}}
{{/each}}
{{/if}}
}{{#newline}}

Usings.mustache

{{#each this}}
using {{this}};{{#newline}}
{{/each}}

Mustache tips

  • Don't forget to use {{#newline}} at the end of lines.
  • Put {{#if}}, {{/if}}, etc. at the start of a line, otherwise, you end up with extra spaces in the generated code.
Clone this wiki locally