-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.Runtime Values
The members of Settings that you read rather than set, plus the helper methods the generated code and your own callbacks can call.
Everything on this page is set by the generator, not by you. Assigning to them is either ignored or actively
harmful. They are documented because the shipped Database.tt uses several of them in its examples, and
because Settings.Root in particular turns up in five other wiki pages with no explanation anywhere.
Type: string
The full path of the folder containing your .tt file. Set by the generator before your settings code
runs, so it is safe to read anywhere in Database.tt.
This is the one you will actually use. Two common cases:
// A SQLite database sitting beside the .tt file
Settings.ConnectionString = "Data Source=" + Path.Combine(Settings.Root, "Northwind.db");The second is what the shipped Database.tt assigns by default. It matters for SQLite especially: a relative
Data Source resolves against the process working directory, which is not your project folder, and SQLite
silently creates an empty database rather than failing. See SQLite.
Type: string
The name of the .tt file being run. Used internally to name the generated output file and to work out the
default namespace. Reading it is harmless; setting it does nothing useful.
Type: string
Default: "Name={ConnectionStringName}"
EF 6 only. What the generated parameterless DbContext constructor passes to its base constructor. It
derives from Settings.ConnectionStringName unless you assign it explicitly.
Set it to null to generate a constructor that does not call the base constructor at all, which is what
you want when a base class of your own supplies the connection:
Settings.DefaultConstructorArgument = null;Has no effect on EF Core, which uses Settings.OnConfiguration instead.
Type: string
The schema treated as the default and therefore never prepended to a class name. The database reader assigns
it, so you normally read it rather than set it - which the shipped Database.tt does throughout its
EnumDefinition and AddRelationship examples. See
Settings.PrependSchemaName for what it affects, including why on SQL Server it is the
login's default schema rather than dbo.
| Database | Default schema | How the reader gets it |
|---|---|---|
| SQL Server | The login's default schema, usually dbo
|
SELECT SCHEMA_NAME(), or dbo if that fails |
| PostgreSQL | public |
Fixed |
| MySQL / MariaDB | The connected database |
SELECT DATABASE(), or mysql if the connection string names no database |
| Oracle | The session's current schema - the connected user, unless ALTER SESSION SET CURRENT_SCHEMA changed it |
SYS_CONTEXT('USERENV','CURRENT_SCHEMA') |
| SQLite | main |
Fixed |
MySQL and Oracle read only that one schema, so every table is in the default schema and nothing is ever prepended.
Use these in a callback when a rule should only apply to some EF versions. They read
Settings.TemplateType, so they are accurate as soon as it is set.
| Helper | Returns true when |
|---|---|
Settings.IsEf6() |
TemplateType is Ef6
|
Settings.IsEfCore8Plus() |
EF Core 8 or later |
Settings.IsEfCore9Plus() |
EF Core 9 or later |
Settings.IsEfCore10Plus() |
EF Core 10 or later |
Settings.EfCoreVersion() |
The major version as an int, or 0 for EF 6 |
Settings.ColumnIdentity = c => Settings.IsEfCore8Plus()
? ".UseIdentityColumn()"
: ".UseSqlServerIdentityColumn()";These answer "did the user ask for partial?", by checking whether the relevant *Modifiers setting contains
the word. The templates use them to decide whether to emit the partial void hooks.
| Helper | Reads |
|---|---|
Settings.DbContextClassIsPartial() |
Settings.DbContextClassModifiers |
Settings.EntityClassesArePartial() |
Settings.EntityClassesModifiers |
Settings.ConfigurationClassesArePartial() |
Settings.ConfigurationClassesModifiers |
DbContextClassIsPartial() is the one worth knowing: if it returns false, no OnModelCreatingPartial hook
is generated, which is why Global Query Filters start by telling you to set
Settings.DbContextClassModifiers = "public partial".
Derived from Settings.DatabaseType, and used by the templates to write the right provider call and the right
parameter syntax.
| Helper | Returns |
|---|---|
Settings.DatabaseProvider() |
The UseXxx provider name, e.g. UseSqlServer
|
Settings.DatabaseProviderAssemblyName() |
The provider assembly, e.g. Microsoft.Data.SqlClient
|
Settings.SqlParameter() |
The parameter type, e.g. SqlParameter
|
Settings.SqlParameterValue() |
How a parameter's value is read back for out parameters |
Settings.NeedsNullForgiving() |
true when AllowNullStrings or NullableReverseNavigationProperties is on, so the templates know to emit = null!
|
You are unlikely to need these unless you are editing the templates inside EF.Reverse.POCO.v4.ttinclude.
Not runtime values, but they belong with the rest of the "call, do not set" members. These are what make
several other settings work, and the shipped Database.tt calls all four at the end of
Settings.UpdateColumn:
| Helper | Makes this setting work |
|---|---|
Settings.ApplyDataAnnotations(column) |
Settings.UseDataAnnotations |
Settings.ApplyEnumTypeReplacement(column, table, enumDefinitions) |
Settings.AddEnumDefinitions |
Settings.ApplyJsonColumnMappings(column, table, jsonColumnMappings) |
Settings.AddJsonColumnMappings |
Settings.ApplyJsonPropertyNameAttribute(column) |
The JsonPropertyName extended property |
Settings.ApplyOwnedEntityMappings(table, ownedEntityMappings) is the table-level equivalent, called from
Settings.UpdateTable.
If you replace UpdateColumn and drop these calls, those settings silently stop working. Nothing errors;
the attributes and type replacements simply never appear. Add your own rules above the helpers, not instead of
them.
-
Settings.UpdateColumn - where the
Apply*helpers are called from -
Settings.OnConfiguration - the EF Core replacement for
DefaultConstructorArgument -
SQLite - why
Settings.Rootmatters more there than anywhere else - Settings Reference
- Home
- Compared with the Microsoft scaffolder
- Connection strings
- JetBrains Rider
- Upgrading from v3 to v4
- Saving .tt does nothing
- 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