Skip to content

Commit

Permalink
loresoft#549 Add option to generate a PK guid value in the entity class
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Boussaer authored and Arnaud Boussaer committed Mar 22, 2024
1 parent ceaa6a5 commit fb9659f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/EntityFrameworkCore.Generator.Core/OptionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
Expand Down Expand Up @@ -79,4 +80,10 @@ public string BaseClass
/// The renaming expressions.
/// </value>
public SelectionOptions Renaming { get; }

/// <summary>
/// If true, the primary key property will have a value generated in the constructor
/// </summary>
[DefaultValue(false)]
public bool GeneratePkValue { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public EntityClass()
RelationshipNaming = RelationshipNaming.Plural;
EntityNaming = EntityNaming.Singular;
PrefixWithSchemaName = false;
GeneratePkValue = false;
}

/// <summary>
Expand Down Expand Up @@ -70,4 +71,10 @@ public EntityClass()
/// The renaming expressions.
/// </value>
public SelectionModel Renaming { get; set; }

/// <summary>
/// If true, the primary key property will have a value generated in the constructor
/// </summary>
[DefaultValue(false)]
public bool GeneratePkValue { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ data:
relationshipNaming: Plural
document: false
prefixWithSchemaName: false
generatePkValue: false
renaming:
entities:
- ^(sp|tbl|udf|vw)_
Expand Down

0 comments on commit fb9659f

Please sign in to comment.