-
Notifications
You must be signed in to change notification settings - Fork 228
Custom file based templates
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.
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.
The latest template files can be found here.
If you are using the multi-context generation (Settings.GenerateSingleDbContext = false
), then please
read this.
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
These fall into two types:
-
txt
files - These contain the usings required for the template. Place each using namespace on a separate line, nousing
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.
Not all examples are listed here, just a few of the smallest ones to give you an idea of what they look like.
{{#each EnumAttributes}}
{{this}}{{#newline}}
{{/each}}
public enum {{EnumName}}{{#newline}}
{{{#newline}}
{{#each Items}}
{{#each Attributes}}
{{this}}{{#newline}}
{{/each}}
{{Key}} = {{Value}},{{#newline}}
{{/each}}
}{{#newline}}
{{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}}
{{#each this}}
using {{this}};{{#newline}}
{{/each}}
- 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.