pyrodistic
Entity Framework - Circular References
Am I stupid or would it might be more efficient to simple create two Dtos for each entity in a way that circular references wouldn't be an issue?
One that references it fully and other to be used when there's an innumerable that references the same property.
32 replies
Entity Framework - Circular References
The thing is that this was a simple example to illustrate the issue. I will have dozens of endpoints, and again, I think there must be a simpler way.
For example, if I wanted to do get the Teams as it currenly is I would have to do:
All because I the mapping can't ignore the circular reference Team - > Person.Team when detected? I'm going to research a little bit further, because I actually think with some effort I can create a mapping algorithm that will just do that automatically... If the property is a class check if it's the same as a parent class, if yes, skip mapping that class.
32 replies
Entity Framework - Circular References
So partially I was being stupid.
There's really no error on the mapping. I was missing FirstOrDefault on the Get call. So the only error is only:
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.
Adding the Json options:
The API request for teams and persons results in:
32 replies
Entity Framework - Circular References
The exception is currently thrown on:
var teamDto = _mapper.Map<TeamDto>(team);
AutoMapper.AutoMapperMappingException
HResult=0x80131500
Message=Missing type map configuration or unsupported mapping.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
If I remove that line and return team I get:
System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Persons.Team.Id.
If I add the Preserve to the JSON the DTOs won't work because JSON adds automatic properties to each entry (id, value). And so on.
32 replies
Entity Framework - Circular References
I'm not though, right? I'm using, or trying to use, the DTO for the endpoint and the entity for the actual DB request, no?
How would I be able, through manual mapping to overcome this issue?
Basically all I really want to do is Team -> Person -> Team for this person is already set, and was the origin so I don't map that one.
32 replies