Skip to content

Settings.DbContextBaseClass

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

Settings.DbContextBaseClass and Settings.DbContextInterfaceBaseClasses

What the generated context inherits from, and what its interface extends.

Settings.DbContextBaseClass Settings.DbContextInterfaceBaseClasses
Type string string
Default "DbContext" "IDisposable"
In Database.tt? Yes Yes

Both are written verbatim into the generated declaration, so they take whatever C# you give them, generic arguments included.

Example

Default

public class MyDbContext : DbContext, IMyDbContext
public interface IMyDbContext : IDisposable

ASP.NET Core Identity

Settings.DbContextBaseClass = "IdentityDbContext<ApplicationUser>";
public class MyDbContext : IdentityDbContext<ApplicationUser>, IMyDbContext

Extra interfaces

Settings.DbContextInterfaceBaseClasses = "IDisposable, IUnitOfWork";
public interface IMyDbContext : IDisposable, IUnitOfWork

When to change them

ASP.NET Core Identity is the overwhelming reason to touch DbContextBaseClass. Identity supplies its own DbSets for users, roles and claims, and its own model configuration, so the context must inherit from IdentityDbContext<TUser> rather than DbContext. Filter out the AspNet* tables while you are at it - the shipped Database.tt already does:

FilterSettings.TableFilters.Add(new RegexExcludeFilter("AspNet.*"));

A base class of your own, holding auditing, soft-delete or multi-tenancy logic that belongs on every context in your solution.

IUnitOfWork or similar on the interface, when your repository layer wants to depend on an abstraction you own rather than on the generated interface directly.

Gotchas

Written verbatim, and not validated. A typo, a missing generic argument or a type that does not exist becomes a compiler error in the generated file. The generator does not know what IdentityDbContext is.

You must supply the namespace. Add it to Settings.AdditionalNamespaces, or write the base class fully qualified:

Settings.AdditionalNamespaces = new List<string> { "Microsoft.AspNetCore.Identity.EntityFrameworkCore" };

A base class with no parameterless constructor breaks the generated constructors. The generator emits public MyDbContext() { } and public MyDbContext(DbContextOptions<MyDbContext> options) : base(options) { }, and both must be satisfiable by your base. IdentityDbContext<TUser> provides both, so the common case works.

DbContextInterfaceBaseClasses is a single string, not a list, despite the plural name. Separate several interfaces with commas inside the one string.

Dropping IDisposable has consequences. Code that does using (var ctx = factory.Create()) against the interface stops compiling. Keep it unless you have a specific reason.

On EF 6 the base class must still be an EF 6 DbContext. The Identity example above is ASP.NET Core; the EF 6 equivalent is IdentityDbContext<ApplicationUser> from Microsoft.AspNet.Identity.EntityFramework, a different type with the same name.

See also

Clone this wiki locally