-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.DbContextBaseClass
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.
public class MyDbContext : DbContext, IMyDbContextpublic interface IMyDbContext : IDisposableSettings.DbContextBaseClass = "IdentityDbContext<ApplicationUser>";public class MyDbContext : IdentityDbContext<ApplicationUser>, IMyDbContextSettings.DbContextInterfaceBaseClasses = "IDisposable, IUnitOfWork";public interface IMyDbContext : IDisposable, IUnitOfWorkASP.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.
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.
- Settings.DbContextName - naming the two types
-
Settings.AdditionalNamespaces - the
usingyour base class needs - Settings.AdditionalContextInterfaceItems - adding members rather than base interfaces
- Filtering - excluding the Identity tables
- 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