Skip to content

Commit 5ab6407

Browse files
committed
fix: inverse loading without inverse relationship attribute
1 parent 86dfd8d commit 5ab6407

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,17 @@ private void LoadInverseRelationships(object trackedRelationshipValue, Relations
198198

199199
private bool IsHasOneRelationship(string internalRelationshipName, Type type)
200200
{
201-
var relationshipAttr = _jsonApiContext.ResourceGraph.GetContextEntity(type).Relationships.Single(r => r.InternalRelationshipName == internalRelationshipName);
202-
if (relationshipAttr is HasOneAttribute) return true;
203-
return false;
201+
var relationshipAttr = _jsonApiContext.ResourceGraph.GetContextEntity(type).Relationships.SingleOrDefault(r => r.InternalRelationshipName == internalRelationshipName);
202+
if(relationshipAttr != null)
203+
{
204+
if (relationshipAttr is HasOneAttribute) return true;
205+
return false;
206+
} else
207+
{
208+
// relationshipAttr is null when we don't put a [RelationshipAttribute] on the inverse navigation property.
209+
// In this case we use relfection to figure out what kind of relationship is pointing back.
210+
return !(type.GetProperty(internalRelationshipName).PropertyType.Inherits(typeof(IEnumerable)));
211+
}
204212
}
205213

206214

0 commit comments

Comments
 (0)