Skip to content

Settings.DbContextName

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

Settings.DbContextName and Settings.DbContextInterfaceName

The names of the two types everything else hangs off.

Settings.DbContextName Settings.DbContextInterfaceName
Type string string
Default "MyDbContext" "I" + DbContextName
In Database.tt? Yes Yes, but commented out

What they do

DbContextName names the generated context class, and several other things follow it: the factory (MyDbContextFactory), the fake context for unit testing (FakeMyDbContext), and by default the interface.

DbContextInterfaceName overrides the interface name independently. Leave it commented out and you get "I" + DbContextName, which is what almost everyone wants.

Example

Default

public interface IMyDbContext : IDisposable
public class MyDbContext : DbContext, IMyDbContext
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
public class Category
public class Product
public class sales_Order
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
public class ProductConfiguration : IEntityTypeConfiguration<Product>
public class sales_OrderConfiguration : IEntityTypeConfiguration<sales_Order>

Settings.DbContextName = "NorthwindData"

public interface INorthwindData : IDisposable
public class NorthwindData : DbContext, INorthwindData
public class NorthwindDataFactory : IDesignTimeDbContextFactory<NorthwindData>
public class Category
public class Product
public class sales_Order
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
public class ProductConfiguration : IEntityTypeConfiguration<Product>
public class sales_OrderConfiguration : IEntityTypeConfiguration<sales_Order>

The context, its interface and its factory all move together.

Overriding just the interface

Settings.DbContextInterfaceName = "INorthwind";
public interface INorthwind : IDisposable
public class MyDbContext : DbContext, INorthwind
public class MyDbContextFactory : IDesignTimeDbContextFactory<MyDbContext>
public class Category
public class Product
public class sales_Order
public class CategoryConfiguration : IEntityTypeConfiguration<Category>
public class ProductConfiguration : IEntityTypeConfiguration<Product>
public class sales_OrderConfiguration : IEntityTypeConfiguration<sales_Order>

Generating no interface at all

Set DbContextInterfaceName to an empty string:

Settings.DbContextInterfaceName = "";

The context is then generated without implementing anything. That is a slightly different thing from removing Elements.Interface from Settings.ElementsToGenerate: dropping the element stops the interface file being written but leaves the context declaring that it implements it, which will not compile unless the interface exists elsewhere. Use the empty string when you want no interface anywhere, and drop the element when the interface is generated by a different .tt in another project.

When to change them

Name the context after the database or the bounded context. MyDbContext is a placeholder, and a solution with two of them is a solution where nobody can tell which is which.

Match your existing convention if you are replacing a hand-written context. Keeping the name means the rest of the codebase does not change.

Set the interface name explicitly when your convention is not IFoo - some teams use FooContract, and some want the interface named for the role rather than the class.

Gotchas

Settings.ConnectionStringName is a different thing with a confusingly similar default. Both default to "MyDbContext". DbContextName is a C# class name; ConnectionStringName is a key looked up in appsettings.json. Renaming one does not rename the other, and they do not have to match. See Connection strings.

The fake context follows the name. FakeMyDbContext becomes FakeNorthwindData, so renaming breaks existing unit tests. See FakeDbContext.

Renaming is a large diff, touching every file that injects the context.

An empty DbContextName generates code that does not compile. There is no fallback.

See also

Clone this wiki locally