From fb9659fbf0c0b86a84106d1a7957e1da922c0d47 Mon Sep 17 00:00:00 2001 From: Arnaud Boussaer Date: Fri, 22 Mar 2024 12:05:02 +0100 Subject: [PATCH] #549 Add option to generate a PK guid value in the entity class --- .../OptionMapper.cs | 1 + .../Options/EntityClassOptions.cs | 7 ++++ .../Serialization/EntityClass.cs | 7 ++++ .../Templates/EntityClassTemplate.cs | 14 ++++++++ .../Templates/MappingClassTemplate.cs | 35 ++++++++++++------- .../Options/full.yaml | 1 + 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs index ad837307..5b938bda 100644 --- a/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs +++ b/src/EntityFrameworkCore.Generator.Core/OptionMapper.cs @@ -154,6 +154,7 @@ private static void MapEntity(EntityClassOptions option, EntityClass entity) option.EntityNaming = entity.EntityNaming; option.RelationshipNaming = entity.RelationshipNaming; option.PrefixWithSchemaName = entity.PrefixWithSchemaName; + option.GeneratePkValue = entity.GeneratePkValue; MapSelection(option.Renaming, entity.Renaming); } diff --git a/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs b/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs index 3878ce7a..1071a54f 100644 --- a/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs +++ b/src/EntityFrameworkCore.Generator.Core/Options/EntityClassOptions.cs @@ -20,6 +20,7 @@ public EntityClassOptions(VariableDictionary variables, string prefix) RelationshipNaming = RelationshipNaming.Plural; EntityNaming = EntityNaming.Singular; PrefixWithSchemaName = false; + GeneratePkValue = false; Renaming = new SelectionOptions(variables, AppendPrefix(prefix, "Naming")); } @@ -79,4 +80,10 @@ public string BaseClass /// The renaming expressions. /// public SelectionOptions Renaming { get; } + + /// + /// If true, the primary key property will have a value generated in the constructor + /// + [DefaultValue(false)] + public bool GeneratePkValue { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs b/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs index 44df5285..71da3b6e 100644 --- a/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs +++ b/src/EntityFrameworkCore.Generator.Core/Serialization/EntityClass.cs @@ -21,6 +21,7 @@ public EntityClass() RelationshipNaming = RelationshipNaming.Plural; EntityNaming = EntityNaming.Singular; PrefixWithSchemaName = false; + GeneratePkValue = false; } /// @@ -70,4 +71,10 @@ public EntityClass() /// The renaming expressions. /// public SelectionModel Renaming { get; set; } + + /// + /// If true, the primary key property will have a value generated in the constructor + /// + [DefaultValue(false)] + public bool GeneratePkValue { get; set; } } diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs index 8bea59bf..42667e61 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/EntityClassTemplate.cs @@ -103,6 +103,20 @@ private void GenerateConstructor() using (CodeBuilder.Indent()) { CodeBuilder.AppendLine("#region Generated Constructor"); + + if (Options.Data.Entity.GeneratePkValue) + { + var primaryKeyProperty = _entity.Properties.FirstOrDefault(p => p.IsPrimaryKey == true); + + if (primaryKeyProperty != null && primaryKeyProperty.DataType == System.Data.DbType.Guid) + { + var primaryKeyPropertyName = primaryKeyProperty.PropertyName.ToSafeName(); + + CodeBuilder.AppendLine($"{primaryKeyPropertyName} = Guid.NewGuid();"); + CodeBuilder.AppendLine(); + } + } + foreach (var relationship in relationships) { var propertyName = relationship.PropertyName.ToSafeName(); diff --git a/src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs b/src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs index 2ae568fe..711945e0 100644 --- a/src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs +++ b/src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs @@ -306,20 +306,29 @@ private void GeneratePropertyMapping(Property property) CodeBuilder.Append($".HasDefaultValueSql({property.Default.ToLiteral()})"); } - switch (property.ValueGenerated) + //If Primary key value should be generated, specify ValueGeneratedNever() so the database won't generate values for the property + if (property.IsPrimaryKey == true && Options.Data.Entity.GeneratePkValue) { - case ValueGenerated.OnAdd: - CodeBuilder.AppendLine(); - CodeBuilder.Append(".ValueGeneratedOnAdd()"); - break; - case ValueGenerated.OnAddOrUpdate: - CodeBuilder.AppendLine(); - CodeBuilder.Append(".ValueGeneratedOnAddOrUpdate()"); - break; - case ValueGenerated.OnUpdate: - CodeBuilder.AppendLine(); - CodeBuilder.Append(".ValueGeneratedOnUpdate()"); - break; + CodeBuilder.AppendLine(); + CodeBuilder.Append(".ValueGeneratedNever()"); + } + else + { + switch (property.ValueGenerated) + { + case ValueGenerated.OnAdd: + CodeBuilder.AppendLine(); + CodeBuilder.Append(".ValueGeneratedOnAdd()"); + break; + case ValueGenerated.OnAddOrUpdate: + CodeBuilder.AppendLine(); + CodeBuilder.Append(".ValueGeneratedOnAddOrUpdate()"); + break; + case ValueGenerated.OnUpdate: + CodeBuilder.AppendLine(); + CodeBuilder.Append(".ValueGeneratedOnUpdate()"); + break; + } } CodeBuilder.DecrementIndent(); diff --git a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml index 5d253569..dc4cad0c 100644 --- a/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml +++ b/test/EntityFrameworkCore.Generator.Core.Tests/Options/full.yaml @@ -34,6 +34,7 @@ data: relationshipNaming: Plural document: false prefixWithSchemaName: false + generatePkValue: false renaming: entities: - ^(sp|tbl|udf|vw)_