Skip to content

Settings.AdditionalFileHeaderText

Simon Hughes edited this page Aug 30, 2026 · 1 revision

Settings.AdditionalFileHeaderText and Settings.AdditionalFileFooterText

Put your own lines verbatim at the top and bottom of every generated file.

AdditionalFileHeaderText AdditionalFileFooterText
Type List<string> List<string>
Default Empty Empty
Position After // <auto-generated>, before the using lines After the namespace closes, before // </auto-generated>
In Database.tt? Yes Yes

What they do

Each list entry becomes one line, written exactly as you give it. Nothing is escaped, prefixed or indented - if you want a comment, write the // yourself.

The header list lands above the using block, which is the only place in a generated file where a file-scoped directive can go.

Example

Settings.AdditionalFileHeaderText = new List<string>
{
    "// Owned by the Platform team",
    "// Do not edit by hand"
};
// <auto-generated>
// Owned by the Platform team
// Do not edit by hand
using Microsoft.Data.SqlClient;

When to use them

Analyser and linter suppressions that need to be at the top of the file:

Settings.AdditionalFileHeaderText = new List<string>
{
    "#pragma warning disable CS8618 // Non-nullable property must contain a non-null value",
    "// <autogenerated />"
};

A #nullable directive finer-grained than Settings.AllowNullStrings gives you:

Settings.AdditionalFileHeaderText = new List<string> { "#nullable disable" };

Licence or provenance headers your organisation requires on every source file.

Coverage exclusions in the footer, for tools that read a trailing marker.

Gotchas

Verbatim means verbatim. A line without // or # is a compiler error, and the error appears in the generated file where the line number means nothing to you.

The header goes before the using lines, so using statements of your own work here - but Settings.AdditionalNamespaces is the right way to add one, because it is sorted in with the rest and deduplicated.

#pragma warning disable written here is never restored, like the one Settings.UsePragma writes. That is fine for generated code and wrong anywhere else.

They apply to every generated file, so with Settings.GenerateSeparateFiles on, a five-line header appears sixty times.

The footer is outside the namespace. It is written after the closing brace, so it cannot contain code that belongs to a type.

See also

Clone this wiki locally