Skip to content

Settings.EntityClassesModifiers

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

Settings.EntityClassesModifiers and the other class modifier settings

Five settings that put whatever you like in front of class - most usefully the word partial.

Type string, all five
Default "public", all five
Applies to EF 6 and EF Core
Databases All
In Database.tt? Yes, as a block of five
Setting Applies to
Settings.EntityClassesModifiers The POCO entity classes
Settings.ConfigurationClassesModifiers The Fluent API configuration classes
Settings.DbContextClassModifiers The DbContext
Settings.DbContextInterfaceModifiers The context interface
Settings.ResultClassModifiers Stored procedure return models

What they do

The string is written verbatim before the type keyword. "public" gives public class Product; "public partial" gives public partial class Product.

In practice you are choosing one thing: partial or not.

Example

"public" (default)

public class Category

"public partial"

public partial class Category

Why partial matters

A generated file is overwritten every time you save the .tt. Anything you add to it is lost. partial is how you add to a generated class without editing it - you write a second file, and the compiler joins them.

// Product.Custom.cs - hand-written, never overwritten
public partial class Product
{
    public string DisplayPrice => UnitPrice.ToString("C");

    public bool IsExpensive() => UnitPrice > 1000m;
}

Settings.DbContextClassModifiers = "public partial" does something extra. It is what makes the generator emit the partial void hooks - OnModelCreatingPartial, InitializePartial, DisposePartial and OnCreateModelPartial. Without partial on the context, those hooks are not generated at all, which is why Global Query Filters and adding your own entities both start by telling you to set it.

When to change them

DbContextClassModifiers = "public partial" for global query filters, soft deletes, multi-tenancy, or adding hand-written entities to the generated context. This is the one most people need.

EntityClassesModifiers = "public partial" for computed properties, ToString() overrides, validation, or interface implementations on entities.

"internal" where the generated code is an implementation detail of an assembly and should not be part of its public surface. Note that all five need changing together, or you will have a public context exposing internal entity types, which does not compile.

ResultClassModifiers = "public partial" when stored procedure result models need behaviour of their own.

Gotchas

The string is not validated. Anything you type goes straight into the declaration. "pubic partial" becomes a compiler error in generated code.

abstract and static will not work. EF has to instantiate entities.

sealed blocks lazy loading. EF Core's proxies subclass your entity at run time, and cannot subclass a sealed class. If you use Settings.UseLazyLoading, leave it unsealed.

Making entities internal but the context public does not compile, because DbSet<Product> on a public context exposes an internal type. Change all five or none.

partial costs nothing when unused. There is no runtime penalty and no code generated for it, so if you think you might want it, turn it on.

See also

Clone this wiki locally