Skip to content

Commit 336b725

Browse files
committed
Do not execute unneeded SQL query in 1-to-1 relationship update, which fails on EF Core 8 (details at dotnet/efcore#31271)
1 parent 6d8e44e commit 336b725

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Diff for: src/JsonApiDotNetCore/Repositories/EntityFrameworkCoreRepository.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,18 @@ protected async Task UpdateRelationshipAsync(RelationshipAttribute relationship,
619619
private bool RequireLoadOfInverseRelationship(RelationshipAttribute relationship, [NotNullWhen(true)] object? trackedValueToAssign)
620620
{
621621
// See https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/502.
622-
return trackedValueToAssign != null && relationship is HasOneAttribute { IsOneToOne: true };
622+
if (trackedValueToAssign != null && relationship is HasOneAttribute { IsOneToOne: true })
623+
{
624+
IEntityType? leftEntityType = _dbContext.Model.FindEntityType(relationship.LeftType.ClrType);
625+
INavigation? navigation = leftEntityType?.FindNavigation(relationship.Property.Name);
626+
627+
if (navigation != null && navigation.ForeignKey.DeclaringEntityType.ClrType == relationship.LeftType.ClrType)
628+
{
629+
return true;
630+
}
631+
}
632+
633+
return false;
623634
}
624635

625636
protected virtual async Task SaveChangesAsync(CancellationToken cancellationToken)

0 commit comments

Comments
 (0)