Skip to content

Settings.AdditionalNamespaces

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

Settings.AdditionalNamespaces and the other "add something extra" settings

Four settings for adding using lines, interface members and blanket attributes to the generated code.

Setting Type Adds
Settings.AdditionalNamespaces List<string> using lines to every generated file
Settings.AdditionalContextInterfaceItems List<string> Members to the DbContext interface
Settings.AdditionalReverseNavigationsDataAnnotations string[] Attributes to every reverse navigation
Settings.AdditionalForeignKeysDataAnnotations string[] Attributes to every foreign key property

All four are in Database.tt and apply to EF 6 and EF Core.

Settings.AdditionalNamespaces

Namespaces to using, without the keyword or the semicolon. They are merged with the ones the generator works out for itself and sorted in.

Settings.AdditionalNamespaces = new List<string>
{
    "System.ComponentModel.DataAnnotations",
    "System.Collections.ObjectModel",
    "MyCompany.Common"
};

You need this whenever another setting names a type the generator does not already reference: a base class in Settings.DbContextBaseClass, ObservableCollection in Settings.CollectionType, an attribute in one of the two arrays below, or anything injected by Settings.WriteInsideClassBody.

Note that Settings.UseDataAnnotations adds the two data annotation namespaces for you, so you do not need to list them for that.

Settings.AdditionalContextInterfaceItems

Extra member declarations on the generated IMyDbContext, written verbatim:

Settings.AdditionalContextInterfaceItems = new List<string>
{
    "void SetAutoDetectChangesEnabled(bool flag);",
    "int ExecuteSqlCommand(string sql, params object[] parameters);"
};

The interface gains them; the context does not. You implement them in a partial class - which means setting Settings.DbContextClassModifiers = "public partial" first. Without that, the generated context does not implement its own interface and will not compile.

The two data annotation arrays

Attribute names without brackets, applied to every reverse navigation property and every foreign key property respectively:

Settings.AdditionalReverseNavigationsDataAnnotations = new[] { "JsonIgnore" };
Settings.AdditionalForeignKeysDataAnnotations        = new[] { "JsonIgnore" };
Settings.AdditionalNamespaces = new List<string> { "System.Text.Json.Serialization" };
    // Category
    public class Category
    {
        public int CategoryId { get; set; } // CategoryId (Primary key)
        public string CategoryName { get; set; } // CategoryName (length: 50)

        // Reverse navigation

        /// <summary>
        /// Child Products where [Product].[CategoryId] point to this entity (FK_Product_Category)
        /// </summary>
        [JsonIgnore]
        public ICollection<Product> Products { get; set; } // Product.FK_Product_Category

        public Category()
        {
            Products = new List<Product>();
        }
    }

This is the blunt instrument for breaking serialisation cycles - every navigation, no exceptions. For per-relationship control use Settings.ForeignKeyAnnotationsProcessing.

Gotchas

All four are written verbatim and none is validated. A typo becomes a compiler error in generated code, pointing at a line number that means nothing in your .tt.

Attributes go without brackets, using lines without the keyword. "[JsonIgnore]" produces [[JsonIgnore]]; "using System;" produces using using System;;.

A using for a namespace nothing references is a warning, not an error, and one that appears in every generated file. Only add what you need.

AdditionalContextInterfaceItems without a partial context does not compile. This catches everyone once.

Newtonsoft.Json and System.Text.Json.Serialization both define JsonIgnore. Reference both and the generated code is ambiguous. Pick one, or write the attribute fully qualified in the array.

The attributes apply to navigation properties, not to entities or scalar columns. For those, use Settings.UpdateColumn and Settings.UpdateTable.

See also

Clone this wiki locally