-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.DbContextName
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 |
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.
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>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.
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>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.
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.
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.
- Settings.ConnectionStringName - the other setting called something-Name
- Settings.ElementsToGenerate - generating the interface elsewhere, or not at all
- Settings.DbContextBaseClass | Settings.AddIDbContextFactory
- FakeDbContext
- Settings Reference
- Settings A-Z - every setting, with a page each
- Common Settings Types Explained
- Settings Callbacks
- Settings runtime values and helpers
- Filtering
- Full Control Over the Generated Code
- Enum Generation from Table Data
- Owned Entities
- JSON column support
- Global Query Filters
- Extended Property Names Feature
- Partial Properties
- File-Scoped Namespaces
- Data Annotations
- Spatial Types
- HierarchyId
- RowVersion and TimeStamp columns
- Lazy Loading
- Stored proc result sets
- Custom File-Based Templates
- Extra entities via partial classes
- INotifyPropertyChanged
- Syntax colour for T4