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 Oct 10, 2024
1 parent ab7c0a8 commit e77c93c
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 e77c93c

Please sign in to comment.