Skip to content

Commit

Permalink
If relationship PrimaryProperties aren't primary key, generate HasPri…
Browse files Browse the repository at this point in the history
…ncipalKey on relationshipo
  • Loading branch information
DavidBoone committed Nov 14, 2024
1 parent 296e690 commit 7ce664a
Showing 1 changed file with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ private void GenerateRelationshipMapping(Relationship relationship)
}
CodeBuilder.Append(")");

var primaryKeys = relationship.PrimaryProperties;
var nonPrimaryPrincipalKey = !primaryKeys
.Select(pp => relationship.PrimaryEntity.Properties.ByProperty(pp.PropertyName))
.All(p => p.IsPrimaryKey ?? true);

if (nonPrimaryPrincipalKey)
{
CodeBuilder.AppendLine();

CodeBuilder.Append(".HasPrincipalKey(t => ");
if (primaryKeys.Count > 1)
{
CodeBuilder.Append("new { ");
CodeBuilder.Append(string.Join(", ", primaryKeys.Select(pp => $"t.{pp.PropertyName.ToSafeName()}")));
CodeBuilder.Append(" }");
}
else
{
var propertyName = primaryKeys.First().PropertyName.ToSafeName();
CodeBuilder.Append($"t.{propertyName}");
}
CodeBuilder.Append(")");
}

if (!string.IsNullOrEmpty(relationship.RelationshipName))
{
CodeBuilder.AppendLine();
Expand Down

0 comments on commit 7ce664a

Please sign in to comment.