-
Notifications
You must be signed in to change notification settings - Fork 224
Enum Generation from Table Data
The generator can create C# enumerations by reading data from database tables. There are two approaches: manual configuration and the AddEnum callback.
Define enumerations directly in Database.tt by specifying which database table to read from.
Given a database table EnumTest.DaysOfWeek:
| TypeName | TypeId |
|---|---|
| Sun | 0 |
| Mon | 1 |
| Tue | 2 |
| Wed | 3 |
| Thu | 4 |
| Fri | 5 |
| Sat | 6 |
Configuration:
Settings.Enumerations = new List<EnumerationSettings>
{
new EnumerationSettings
{
Name = "DaysOfWeek", // Name of the generated enum
Table = "EnumTest.DaysOfWeek", // Schema.TableName
NameField = "TypeName", // Column for enum member names
ValueField = "TypeId" // Column for enum member values
}
};Generated code:
public enum DaysOfWeek
{
Sun = 0,
Mon = 1,
Tue = 2,
Wed = 3,
Thu = 4,
Fri = 5,
Sat = 6
}Given a table car_options:
| enum_name | value |
|---|---|
| SunRoof | 1 |
| Spoiler | 2 |
| FogLights | 4 |
| TintedWindows | 8 |
Configuration:
new EnumerationSettings
{
Name = "CarOptions",
Table = "car_options",
NameField = "enum_name",
ValueField = "value"
}Generated code:
public enum CarOptions
{
SunRoof = 0x01,
Spoiler = 0x02,
FogLights = 0x04,
TintedWindows = 0x08
}Option A: Description from a database column
new EnumerationSettings
{
Name = "DaysOfWeek",
Table = "EnumTest.DaysOfWeek",
NameField = "TypeName",
ValueField = "TypeId",
DescriptionField = "Description" // Column containing the description text
}Generated code:
public enum DaysOfWeek
{
[Description("Sunday")] Sun = 0,
[Description("Monday")] Mon = 1,
[Description("Tuesday")] Tue = 2,
// ...
}Option B: Generate description automatically from the enum member name
new EnumerationSettings
{
Name = "CarOptions",
Table = "car_options",
NameField = "enum_name",
ValueField = "value",
GenerateDescriptionFromName = true // Converts "SunRoof" -> "Sun roof"
}Generated code:
public enum CarOptions
{
[Description("Sun roof")] SunRoof = 0x01,
[Description("Spoiler")] Spoiler = 0x02,
[Description("Fog lights")] FogLights = 0x04,
[Description("Tinted windows")] TintedWindows = 0x08
}Both options combined: If DescriptionField is set but a row has a null/empty description, and GenerateDescriptionFromName = true, the generated name is used as a fallback.
If a single database table contains multiple enumerations differentiated by a group column, use GroupField:
| GroupName | MemberName | MemberValue |
|---|---|---|
| Color | Red | 1 |
| Color | Green | 2 |
| Color | Blue | 3 |
| Size | Small | 1 |
| Size | Medium | 2 |
| Size | Large | 3 |
Configuration:
new EnumerationSettings
{
Name = "{GroupField}Enum", // {GroupField} is replaced with the group value
Table = "dbo.EnumValues",
NameField = "MemberName",
ValueField = "MemberValue",
GroupField = "GroupName" // Column that identifies the enum group
}Generated code:
public enum ColorEnum { Red = 1, Green = 2, Blue = 3 }
public enum SizeEnum { Small = 1, Medium = 2, Large = 3 }| Property | Type | Required | Description |
|---|---|---|---|
Name |
string |
Yes | Name of the generated enum. Use {GroupField} placeholder if grouping. |
Table |
string |
Yes | Database table in Schema.TableName format. |
NameField |
string |
Yes | Column containing enum member names. |
ValueField |
string |
Yes | Column containing enum member values. |
GroupField |
string |
No | Column for grouping multiple enums in one table. |
DescriptionField |
string |
No | Column containing [Description] text. |
GenerateDescriptionFromName |
bool |
No | If true, auto-generates [Description] from the member name when no description is available. |
The AddEnum callback is invoked for each table during reverse engineering. Use it to programmatically decide which tables should become enums — for example, tables matching a naming convention.
Requires Settings.ElementsToGenerate to include both Elements.Poco and Elements.Enum.
Settings.AddEnum = delegate (Table table)
{
if (table.HasPrimaryKey &&
table.PrimaryKeys.Count() == 1 &&
table.Columns.Any(x => x.PropertyType == "string"))
{
// Convert tables named "XxxEnum" or "EnumXxx" into enumerations
if (table.NameHumanCase.StartsWith("Enum", StringComparison.InvariantCultureIgnoreCase) ||
table.NameHumanCase.EndsWith ("Enum", StringComparison.InvariantCultureIgnoreCase))
{
try
{
Settings.Enumerations.Add(new EnumerationSettings
{
Name = table.NameHumanCase.Replace("Enum", "") + "Enum",
Table = table.Schema.DbName + "." + table.DbName,
NameField = table.Columns.First(x => x.PropertyType == "string").DbName,
ValueField = table.PrimaryKeys.Single().DbName,
GroupField = string.Empty
});
// Stop the table from being added to the DbContext
table.RemoveTable = true;
}
catch { }
}
}
};Remove table.RemoveTable = true; if you want to keep the table as a POCO in addition to generating the enum.