-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.AdditionalNamespaces
Four settings for adding using lines, interface members and blanket attributes to the generated code.
| Setting | Type | Adds |
|---|---|---|
Settings.AdditionalNamespaces |
List<string> |
using lines to every generated file |
Settings.AdditionalContextInterfaceItems |
List<string> |
Members to the DbContext interface |
Settings.AdditionalReverseNavigationsDataAnnotations |
string[] |
Attributes to every reverse navigation |
Settings.AdditionalForeignKeysDataAnnotations |
string[] |
Attributes to every foreign key property |
All four are in Database.tt and apply to EF 6 and EF Core.
Namespaces to using, without the keyword or the semicolon. They are merged with the ones the generator
works out for itself and sorted in.
Settings.AdditionalNamespaces = new List<string>
{
"System.ComponentModel.DataAnnotations",
"System.Collections.ObjectModel",
"MyCompany.Common"
};You need this whenever another setting names a type the generator does not already reference: a base class in
Settings.DbContextBaseClass, ObservableCollection in
Settings.CollectionType, an attribute in one of the two arrays below, or
anything injected by Settings.WriteInsideClassBody.
Note that Settings.UseDataAnnotations adds the two data annotation namespaces
for you, so you do not need to list them for that.
Extra member declarations on the generated IMyDbContext, written verbatim:
Settings.AdditionalContextInterfaceItems = new List<string>
{
"void SetAutoDetectChangesEnabled(bool flag);",
"int ExecuteSqlCommand(string sql, params object[] parameters);"
};The interface gains them; the context does not. You implement them in a partial class - which means setting
Settings.DbContextClassModifiers = "public partial" first. Without that, the generated context does not
implement its own interface and will not compile.
Attribute names without brackets, applied to every reverse navigation property and every foreign key property respectively:
Settings.AdditionalReverseNavigationsDataAnnotations = new[] { "JsonIgnore" };
Settings.AdditionalForeignKeysDataAnnotations = new[] { "JsonIgnore" };
Settings.AdditionalNamespaces = new List<string> { "System.Text.Json.Serialization" }; // Category
public class Category
{
public int CategoryId { get; set; } // CategoryId (Primary key)
public string CategoryName { get; set; } // CategoryName (length: 50)
// Reverse navigation
/// <summary>
/// Child Products where [Product].[CategoryId] point to this entity (FK_Product_Category)
/// </summary>
[JsonIgnore]
public ICollection<Product> Products { get; set; } // Product.FK_Product_Category
public Category()
{
Products = new List<Product>();
}
}This is the blunt instrument for breaking serialisation cycles - every navigation, no exceptions. For
per-relationship control use
Settings.ForeignKeyAnnotationsProcessing.
All four are written verbatim and none is validated. A typo becomes a compiler error in generated code,
pointing at a line number that means nothing in your .tt.
Attributes go without brackets, using lines without the keyword. "[JsonIgnore]" produces
[[JsonIgnore]]; "using System;" produces using using System;;.
A using for a namespace nothing references is a warning, not an error, and one that appears in every
generated file. Only add what you need.
AdditionalContextInterfaceItems without a partial context does not compile. This catches everyone once.
Newtonsoft.Json and System.Text.Json.Serialization both define JsonIgnore. Reference both and the
generated code is ambiguous. Pick one, or write the attribute fully qualified in the array.
The attributes apply to navigation properties, not to entities or scalar columns. For those, use
Settings.UpdateColumn and Settings.UpdateTable.
- Settings.ForeignKeyAnnotationsProcessing - per-relationship attributes
- Settings.UpdateColumn - per-column attributes
-
Settings.EntityClassesModifiers - the
partialthe interface items need - Data Annotations
- 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