Skip to content

Commit 6e7ff58

Browse files
committed
#867 Add support for partial properties. Thanks to Statler.
1 parent e0cb2de commit 6e7ff58

File tree

15 files changed

+168
-44
lines changed

15 files changed

+168
-44
lines changed

BuildTT/BuildTT.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ private static void CreateTT(string generatorRoot, string ttRoot)
380380
// column.OverrideModifier = true;
381381
// This will create: public override long id { get; set; }
382382
383+
// Make property partial, see https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/wiki/Partial-properties
384+
//if (table.NameHumanCase.Equals(""SomeTable"", StringComparison.InvariantCultureIgnoreCase) && column.NameHumanCase.Equals(""SomeColumn"", StringComparison.InvariantCultureIgnoreCase))
385+
// column.IsPartial = true;
386+
383387
if (Settings.UseDataAnnotations)
384388
{
385389
if (column.IsPrimaryKey)

EntityFramework.Reverse.POCO.Generator/Database.tt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,10 @@
364364
// column.OverrideModifier = true;
365365
// This will create: public override long id { get; set; }
366366

367+
// Make property partial, see https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/wiki/Partial-properties
368+
//if (table.NameHumanCase.Equals("SomeTable", StringComparison.InvariantCultureIgnoreCase) && column.NameHumanCase.Equals("SomeColumn", StringComparison.InvariantCultureIgnoreCase))
369+
// column.IsPartial = true;
370+
367371
if (Settings.UseDataAnnotations)
368372
{
369373
if (column.IsPrimaryKey)

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,40 @@
369369
// column.OverrideModifier = true;
370370
// This will create: public override long id { get; set; }
371371

372+
// Make property partial, see https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/wiki/Partial-properties
373+
//if (table.NameHumanCase.Equals("SomeTable", StringComparison.InvariantCultureIgnoreCase) && column.NameHumanCase.Equals("SomeColumn", StringComparison.InvariantCultureIgnoreCase))
374+
// column.IsPartial = true;
375+
376+
if (UseDataAnnotations)
377+
{
378+
if (column.IsPrimaryKey)
379+
column.Attributes.Add(string.Format("[Key, Column(Order = {0})]", column.Ordinal));
380+
381+
if (column.IsMaxLength)
382+
column.Attributes.Add("[MaxLength]");
383+
384+
if (column.IsRowVersion)
385+
column.Attributes.Add("[Timestamp, ConcurrencyCheck]");
386+
387+
if (!column.IsMaxLength && column.MaxLength > 0)
388+
{
389+
var doNotSpecifySize = (DatabaseType == DatabaseType.SqlCe && column.MaxLength > 4000);
390+
column.Attributes.Add(doNotSpecifySize ? "[MaxLength]" : string.Format("[MaxLength({0})]", column.MaxLength));
391+
if (column.PropertyType.Equals("string", StringComparison.InvariantCultureIgnoreCase))
392+
column.Attributes.Add(string.Format("[StringLength({0})]", column.MaxLength));
393+
}
394+
395+
if (!column.IsNullable && !column.IsComputed)
396+
{
397+
if (column.PropertyType.Equals("string", StringComparison.InvariantCultureIgnoreCase) && column.AllowEmptyStrings)
398+
column.Attributes.Add("[Required(AllowEmptyStrings = true)]");
399+
else
400+
column.Attributes.Add("[Required]");
401+
}
402+
403+
column.Attributes.Add(string.Format("[Display(Name = \"{0}\")]", column.DisplayName));
404+
}
405+
372406
// Perform Enum property type replacement
373407
var enumDefinition = enumDefinitions?.FirstOrDefault(e =>
374408
(e.Schema.Equals(table.Schema.DbName, StringComparison.InvariantCultureIgnoreCase)) &&
@@ -1222,6 +1256,7 @@
12221256
public bool IsMaxLength;
12231257
public bool IsForeignKey;
12241258
public bool IsSpatial;
1259+
public bool IsPartial;
12251260

12261261
public string Config;
12271262
public List<string> ConfigFk = new List<string>();
@@ -3935,7 +3970,9 @@
39353970
HasNoPrimaryKey = !table.HasPrimaryKey,
39363971
Name = table.DbName,
39373972
NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(),
3938-
ClassModifier = Settings.EntityClassesModifiers,
3973+
ClassModifier = columnsQuery
3974+
.Where(x => !x.Hidden && !x.ExistsInBaseClass)
3975+
.Any(x => x.IsPartial) ? "public partial" : Settings.EntityClassesModifiers,
39393976
ClassComment = table.WriteComments(),
39403977
ExtendedComments = table.WriteExtendedComments(),
39413978
ClassAttributes = table.WriteClassAttributes(),
@@ -3956,7 +3993,8 @@
39563993
NameHumanCase = col.NameHumanCase,
39573994
PrivateSetterForComputedColumns = Settings.UsePrivateSetterForComputedColumns && col.IsComputed ? "private " : string.Empty,
39583995
PropertyInitialisers = Settings.UsePropertyInitialisers ? (string.IsNullOrWhiteSpace(col.Default) ? string.Empty : string.Format(" = {0};", col.Default)) : string.Empty,
3959-
InlineComments = col.InlineComments
3996+
InlineComments = col.InlineComments,
3997+
IsPartial = col.IsPartial
39603998
})
39613999
.ToList(),
39624000
HasReverseNavigation = table.ReverseNavigationProperty.Count > 0,
@@ -17647,6 +17685,7 @@ SELECT SERVERPROPERTY('Edition') AS Edition,
1764717685
public string PrivateSetterForComputedColumns { get; set; }
1764817686
public string PropertyInitialisers { get; set; }
1764917687
public string InlineComments { get; set; }
17688+
public bool IsPartial { get; set; }
1765017689
}
1765117690

1765217691
public class PocoReverseNavigationPropertyModel
@@ -18847,7 +18886,7 @@ using {{this}};{{#newline}}
1884718886
{{#each Attributes}}
1884818887
{{this}}{{#newline}}
1884918888
{{/each}}
18850-
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
18889+
public {{#if OverrideModifier}}override {{/if}}{{#if IsPartial}}partial {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1885118890
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
1885218891
{{/each}}
1885318892

@@ -20522,7 +20561,7 @@ public class FakeDbContextTransaction : IDbContextTransaction{{#newline}}
2052220561
{{#each Attributes}}
2052320562
{{this}}{{#newline}}
2052420563
{{/each}}
20525-
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
20564+
public {{#if OverrideModifier}}override {{/if}}{{#if IsPartial}}partial {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
2052620565
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
2052720566
{{/each}}
2052820567

Generator/Column.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class Column : EntityName
4848
public bool IsMaxLength;
4949
public bool IsForeignKey;
5050
public bool IsSpatial;
51+
public bool IsPartial;
5152

5253
public string Config;
5354
public List<string> ConfigFk = new List<string>();

Generator/Generators/CodeGenerator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,9 @@ public CodeOutput GeneratePoco(Table table)
484484
HasNoPrimaryKey = !table.HasPrimaryKey,
485485
Name = table.DbName,
486486
NameHumanCaseWithSuffix = table.NameHumanCaseWithSuffix(),
487-
ClassModifier = Settings.EntityClassesModifiers,
487+
ClassModifier = columnsQuery
488+
.Where(x => !x.Hidden && !x.ExistsInBaseClass)
489+
.Any(x => x.IsPartial) ? "public partial" : Settings.EntityClassesModifiers,
488490
ClassComment = table.WriteComments(),
489491
ExtendedComments = table.WriteExtendedComments(),
490492
ClassAttributes = table.WriteClassAttributes(),
@@ -505,7 +507,8 @@ public CodeOutput GeneratePoco(Table table)
505507
NameHumanCase = col.NameHumanCase,
506508
PrivateSetterForComputedColumns = Settings.UsePrivateSetterForComputedColumns && col.IsComputed ? "private " : string.Empty,
507509
PropertyInitialisers = Settings.UsePropertyInitialisers ? (string.IsNullOrWhiteSpace(col.Default) ? string.Empty : string.Format(" = {0};", col.Default)) : string.Empty,
508-
InlineComments = col.InlineComments
510+
InlineComments = col.InlineComments,
511+
IsPartial = col.IsPartial
509512
})
510513
.ToList(),
511514
HasReverseNavigation = table.ReverseNavigationProperty.Count > 0,

Generator/Settings.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,40 @@ public static class Settings
343343
// column.OverrideModifier = true;
344344
// This will create: public override long id { get; set; }
345345

346+
// Make property partial, see https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator/wiki/Partial-properties
347+
//if (table.NameHumanCase.Equals("SomeTable", StringComparison.InvariantCultureIgnoreCase) && column.NameHumanCase.Equals("SomeColumn", StringComparison.InvariantCultureIgnoreCase))
348+
// column.IsPartial = true;
349+
350+
if (UseDataAnnotations)
351+
{
352+
if (column.IsPrimaryKey)
353+
column.Attributes.Add(string.Format("[Key, Column(Order = {0})]", column.Ordinal));
354+
355+
if (column.IsMaxLength)
356+
column.Attributes.Add("[MaxLength]");
357+
358+
if (column.IsRowVersion)
359+
column.Attributes.Add("[Timestamp, ConcurrencyCheck]");
360+
361+
if (!column.IsMaxLength && column.MaxLength > 0)
362+
{
363+
var doNotSpecifySize = (DatabaseType == DatabaseType.SqlCe && column.MaxLength > 4000);
364+
column.Attributes.Add(doNotSpecifySize ? "[MaxLength]" : string.Format("[MaxLength({0})]", column.MaxLength));
365+
if (column.PropertyType.Equals("string", StringComparison.InvariantCultureIgnoreCase))
366+
column.Attributes.Add(string.Format("[StringLength({0})]", column.MaxLength));
367+
}
368+
369+
if (!column.IsNullable && !column.IsComputed)
370+
{
371+
if (column.PropertyType.Equals("string", StringComparison.InvariantCultureIgnoreCase) && column.AllowEmptyStrings)
372+
column.Attributes.Add("[Required(AllowEmptyStrings = true)]");
373+
else
374+
column.Attributes.Add("[Required]");
375+
}
376+
377+
column.Attributes.Add(string.Format("[Display(Name = \"{0}\")]", column.DisplayName));
378+
}
379+
346380
// Perform Enum property type replacement
347381
var enumDefinition = enumDefinitions?.FirstOrDefault(e =>
348382
(e.Schema.Equals(table.Schema.DbName, StringComparison.InvariantCultureIgnoreCase)) &&

Generator/TemplateModels/PocoModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class PocoColumnModel
4141
public string PrivateSetterForComputedColumns { get; set; }
4242
public string PropertyInitialisers { get; set; }
4343
public string InlineComments { get; set; }
44+
public bool IsPartial { get; set; }
4445
}
4546

4647
public class PocoReverseNavigationPropertyModel

Generator/Templates/TemplateEf6.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ public override string Poco()
11111111
{{#each Attributes}}
11121112
{{this}}{{#newline}}
11131113
{{/each}}
1114-
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1114+
public {{#if OverrideModifier}}override {{/if}}{{#if IsPartial}}partial {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
11151115
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
11161116
{{/each}}
11171117

Generator/Templates/TemplateEfCore8.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ public override string Poco()
14551455
{{#each Attributes}}
14561456
{{this}}{{#newline}}
14571457
{{/each}}
1458-
public {{#if OverrideModifier}}override {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
1458+
public {{#if OverrideModifier}}override {{/if}}{{#if IsPartial}}partial {{/if}}{{WrapIfNullable}} {{NameHumanCase}} { get; {{PrivateSetterForComputedColumns}}set; }{{PropertyInitialisers}}{{InlineComments}}{{#newline}}
14591459
{{#if IncludeFieldNameConstants}} public const string {{NameHumanCase}}Field = ""{{NameHumanCase}}"";{{#newline}}{{/if}}
14601460
{{/each}}
14611461
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace V9EfrpgTest;
2+
3+
public partial class AbOrderLinesAb
4+
{
5+
private string _sku = string.Empty;
6+
public partial string Sku
7+
{
8+
get => _sku;
9+
set => _sku = value;
10+
}
11+
}

0 commit comments

Comments
 (0)