Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #549 Add option to generate a PK guid value in the entity class #550

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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