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

Generate HasPrincipalKey on relationship if necessary #575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure what IsPrimaryKey == null would mean, so I used ?? true so it would not generate the .HasPrincipalKey stuff in that case. There may be other corner-cases I haven't considered as well.


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
Loading