Skip to content

Commit 24cbdd5

Browse files
committed
#741 IncludeFieldNameConstants. Thanks to MarkLFT.
1 parent e277623 commit 24cbdd5

File tree

22 files changed

+794
-14
lines changed

22 files changed

+794
-14
lines changed

BuildTT/BuildTT.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private static void CreateTT(string generatorRoot, string ttRoot)
121121
Settings.UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
122122
Settings.IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code (Settings.ShowLicenseInfo must also be true).
123123
Settings.TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
124+
Settings.IncludeFieldNameConstants = false; // Will include public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}""; in the generated POCO class. It allows you to use a constant instead of magic strings.
124125
Settings.AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. new List<string> { ""Microsoft.AspNetCore.Identity.EntityFrameworkCore"", ""System.ComponentModel.DataAnnotations"" };
125126
Settings.AdditionalContextInterfaceItems = new List<string>(); // example: new List<string> { ""void SetAutoDetectChangesEnabled(bool flag);"" };
126127
Settings.AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry

EntityFramework.Reverse.POCO.Generator/Database.tt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
Settings.UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
106106
Settings.IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code (Settings.ShowLicenseInfo must also be true).
107107
Settings.TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
108+
Settings.IncludeFieldNameConstants = false; // Will include public const string {{NameHumanCase}}Field = "{{NameHumanCase}}"; in the generated POCO class. It allows you to use a constant instead of magic strings.
108109
Settings.AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. new List<string> { "Microsoft.AspNetCore.Identity.EntityFrameworkCore", "System.ComponentModel.DataAnnotations" };
109110
Settings.AdditionalContextInterfaceItems = new List<string>(); // example: new List<string> { "void SetAutoDetectChangesEnabled(bool flag);" };
110111
Settings.AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
public static bool UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
9999
public static bool IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code (Settings.ShowLicenseInfo must also be true).
100100
public static bool TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
101+
public static bool IncludeFieldNameConstants = false; // Will include public const string {{NameHumanCase}}Field = "{{NameHumanCase}}"; in the generated POCO class. It allows you to use a constant instead of magic strings.
101102
public static List<string> AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. "Microsoft.AspNet.Identity.EntityFramework"
102103
public static List<string> AdditionalContextInterfaceItems = new List<string>(); // example: "void SetAutoDetectChangesEnabled(bool flag);"
103104
public static List<string> AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry
@@ -3893,6 +3894,7 @@
38933894
SummaryComments = !string.IsNullOrEmpty(col.SummaryComments) ? SecurityElement.Escape(col.SummaryComments) : null,
38943895
Attributes = col.Attributes,
38953896
OverrideModifier = col.OverrideModifier,
3897+
IncludeFieldNameConstants = Settings.IncludeFieldNameConstants,
38963898
WrapIfNullable = col.WrapIfNullable(),
38973899
NameHumanCase = col.NameHumanCase,
38983900
PrivateSetterForComputedColumns = Settings.UsePrivateSetterForComputedColumns && col.IsComputed ? "private " : string.Empty,
@@ -17351,6 +17353,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
1735117353
public string SummaryComments { get; set; }
1735217354
public List<string> Attributes { get; set; }
1735317355
public bool OverrideModifier { get; set; }
17356+
public bool IncludeFieldNameConstants { get; set; }
1735417357
public string WrapIfNullable { get; set; }
1735517358
public string NameHumanCase { get; set; }
1735617359
public string PrivateSetterForComputedColumns { get; set; }
@@ -18543,6 +18546,7 @@ using {{this}};{{#newline}}
1854318546
{{this}}{{#newline}}
1854418547
{{/each}}
1854518548
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
18549+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
1854618550
{{/each}}
1854718551

1854818552
{{#if HasReverseNavigation}}
@@ -20032,6 +20036,7 @@ public class FakeDbContextTransaction : IDbContextTransaction{{#newline}}
2003220036
{{this}}{{#newline}}
2003320037
{{/each}}
2003420038
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
20039+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
2003520040
{{/each}}
2003620041

2003720042
{{#if HasReverseNavigation}}
@@ -21616,6 +21621,7 @@ public class FakeDbContextTransaction : IDbContextTransaction{{#newline}}
2161621621
{{this}}{{#newline}}
2161721622
{{/each}}
2161821623
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
21624+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
2161921625
{{/each}}
2162021626

2162121627
{{#if HasReverseNavigation}}
@@ -23248,6 +23254,7 @@ public class FakeDbContextTransaction : IDbContextTransaction{{#newline}}
2324823254
{{this}}{{#newline}}
2324923255
{{/each}}
2325023256
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
23257+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
2325123258
{{/each}}
2325223259

2325323260
{{#if HasReverseNavigation}}
@@ -24885,6 +24892,7 @@ public class FakeDbContextTransaction : IDbContextTransaction{{#newline}}
2488524892
{{this}}{{#newline}}
2488624893
{{/each}}
2488724894
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
24895+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
2488824896
{{/each}}
2488924897

2489024898
{{#if HasReverseNavigation}}
@@ -26529,6 +26537,7 @@ public class FakeDbContextTransaction : IDbContextTransaction{{#newline}}
2652926537
{{this}}{{#newline}}
2653026538
{{/each}}
2653126539
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
26540+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
2653226541
{{/each}}
2653326542

2653426543
{{#if HasReverseNavigation}}

Generator/Generators/CodeGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ public CodeOutput GeneratePoco(Table table)
492492
SummaryComments = !string.IsNullOrEmpty(col.SummaryComments) ? SecurityElement.Escape(col.SummaryComments) : null,
493493
Attributes = col.Attributes,
494494
OverrideModifier = col.OverrideModifier,
495+
IncludeFieldNameConstants = Settings.IncludeFieldNameConstants,
495496
WrapIfNullable = col.WrapIfNullable(),
496497
NameHumanCase = col.NameHumanCase,
497498
PrivateSetterForComputedColumns = Settings.UsePrivateSetterForComputedColumns && col.IsComputed ? "private " : string.Empty,

Generator/Settings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static class Settings
7070
public static bool UsePrivateSetterForComputedColumns = true; // If the columns is computed, use a private setter.
7171
public static bool IncludeGeneratorVersionInCode = false; // If true, will include the version number of the generator in the generated code (Settings.ShowLicenseInfo must also be true).
7272
public static bool TrimCharFields = false; // EF Core option only. If true, will TrimEnd() 'char' fields when read from the database.
73+
public static bool IncludeFieldNameConstants = false; // Will include public const string {{NameHumanCase}}Field = "{{NameHumanCase}}"; in the generated POCO class. It allows you to use a constant instead of magic strings.
7374
public static List<string> AdditionalNamespaces = new List<string>(); // To include extra namespaces, include them here. i.e. "Microsoft.AspNet.Identity.EntityFramework"
7475
public static List<string> AdditionalContextInterfaceItems = new List<string>(); // example: "void SetAutoDetectChangesEnabled(bool flag);"
7576
public static List<string> AdditionalFileHeaderText = new List<string>(); // This will put additional lines verbatim at the top of each file under the comments, 1 line per entry

Generator/TemplateModels/PocoModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class PocoColumnModel
3434
public string SummaryComments { get; set; }
3535
public List<string> Attributes { get; set; }
3636
public bool OverrideModifier { get; set; }
37+
public bool IncludeFieldNameConstants { get; set; }
3738
public string WrapIfNullable { get; set; }
3839
public string NameHumanCase { get; set; }
3940
public string PrivateSetterForComputedColumns { get; set; }

Generator/Templates/TemplateEf6.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ public override string Poco()
10991099
{{this}}{{#newline}}
11001100
{{/each}}
11011101
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1102+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
11021103
{{/each}}
11031104
11041105
{{#if HasReverseNavigation}}

Generator/Templates/TemplateEfCore2.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ public override string Poco()
12771277
{{this}}{{#newline}}
12781278
{{/each}}
12791279
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1280+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
12801281
{{/each}}
12811282
12821283
{{#if HasReverseNavigation}}

Generator/Templates/TemplateEfCore3.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,6 +1368,7 @@ public override string Poco()
13681368
{{this}}{{#newline}}
13691369
{{/each}}
13701370
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1371+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
13711372
{{/each}}
13721373
13731374
{{#if HasReverseNavigation}}

Generator/Templates/TemplateEfCore5.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,7 @@ public override string Poco()
14161416
{{this}}{{#newline}}
14171417
{{/each}}
14181418
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1419+
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
14191420
{{/each}}
14201421
14211422
{{#if HasReverseNavigation}}

0 commit comments

Comments
 (0)