Skip to content
Merged
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 @@
}
CodeBuilder.Append(")");

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

Check warning on line 230 in src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 230 in src/EntityFrameworkCore.Generator.Core/Templates/MappingClassTemplate.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
Copy link
Contributor 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