-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.EntityClassesModifiers
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 |
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.
public class Categorypublic partial class CategoryA 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.
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.
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.
- Global Query Filters - the main reason to make the context partial
- Extra entities via partial classes
- Settings.WriteInsideClassBody - adding members without a partial file
- Settings.ConfigurationClassName
- 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