-
Notifications
You must be signed in to change notification settings - Fork 226
Settings.Namespace
Which namespace the generated code lives in, how it is written, and how to point one project's generated code at another's.
| Setting | Type | Default |
|---|---|---|
Settings.Namespace |
string |
DefaultNamespace |
Settings.UseNamespace |
bool |
true |
Settings.UseFileScopedNamespaces |
bool |
false |
Settings.UseFolderNameInNamespace |
bool |
false |
Settings.ContextNamespace |
string |
"" |
Settings.InterfaceNamespace |
string |
"" |
Settings.PocoNamespace |
string |
"" |
Settings.PocoConfigurationNamespace |
string |
"" |
The namespace everything generated goes into.
The default, DefaultNamespace, is a variable the T4 host supplies - it resolves to your project's root
namespace. To override it, use a quoted string:
Settings.Namespace = "MyCompany.MyProject.Data";If your generated namespace changed when you upgraded to v4, this is where to pin it. v4 asks the T4 host for the root namespace, where v3 went through the Visual Studio automation model, and the two can disagree.
false suppresses the namespace declaration entirely, so the generated types land in the global namespace.
Rarely useful. It exists for single-file scripting scenarios and for templates that wrap the output in their
own namespace. Turning it off also disables
Settings.UseFileScopedNamespaces, because there is no declaration to change the
style of.
namespace MyApp.Data; instead of namespace MyApp.Data { ... }, removing a level of indentation from every
generated file.
namespace MyApp.Databecomes
namespace MyApp.Data;Requires C# 10, which needs <LangVersion>10</LangVersion> or later - available on net48 too, as long as
the project is SDK-style. Full detail on File-Scoped Namespaces.
These do not move any files. They add using statements, so generated code in one namespace can see
generated code in another.
You need them when you split generation across projects with
Settings.ElementsToGenerate:
// MyApp.Entities\Entities.tt
Settings.ElementsToGenerate = Elements.Poco;
Settings.Namespace = "MyApp.Entities";
// MyApp.Data\Data.tt
Settings.ElementsToGenerate = Elements.Context | Elements.Interface | Elements.PocoConfiguration;
Settings.Namespace = "MyApp.Data";
Settings.PocoNamespace = "MyApp.Entities"; // <-- the using the context needsWithout that last line the DbContext refers to Product and cannot find it.
Set the one that names where the other thing lives:
| Setting | Says where to find |
|---|---|
Settings.PocoNamespace |
The entity classes |
Settings.ContextNamespace |
The DbContext
|
Settings.InterfaceNamespace |
The context interface |
Settings.PocoConfigurationNamespace |
The configuration classes |
DefaultNamespace is not a string, and not quoted. It is a T4 variable. Settings.Namespace = DefaultNamespace;
is correct; Settings.Namespace = "DefaultNamespace"; puts your code in a namespace literally called that.
UseFolderNameInNamespace and the four cross-namespace settings are unrelated. The first makes the
namespace follow the output folder; the others add using lines. Turning the first on changes what the others
need to point at.
Both projects must be regenerated together. They are two independent runs against the same database, and if one is stale the entity classes and the configuration classes stop agreeing.
A namespace is usually better than Settings.TableSuffix for avoiding a clash
with existing types - it costs one using rather than putting Dto in front of every reader forever.
- File-Scoped Namespaces - the full page, with FAQ
- Settings.ElementsToGenerate - the reason the cross-namespace settings exist
- Settings.PocoFolder and the folder settings
- Upgrading from v3 to v4 - if your namespace changed
- 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