C
C#ā€¢9mo ago
Spugnetta

Avoiding circular references in navigation properties

Hi guys šŸ™‚ i have a question, let's say i have two entities with a one to one relationship, and both have a navigation prop to the other, when creating that entity what's the best way to avoid circular references?
6 Replies
Jimmacle
Jimmacleā€¢9mo ago
creating it how? circular references are expected with bidirectional navigation properties and that is also how EF will populate them when requested from the database that shouldn't matter unless you're trying to serialize your entities, which you shouldn't be
Spugnetta
SpugnettaOPā€¢9mo ago
thx for the answer, can i ask you more about this? also with creating i meant for example creating the author, then creating the post and then assign them to each other, and then add them to db
Jimmacle
Jimmacleā€¢9mo ago
you can just assign one side of the relationship and EF will figure it out when you call SaveChanges
Spugnetta
SpugnettaOPā€¢9mo ago
how can i retrive this autor without getting a json circular exception for example?
No description
Jimmacle
Jimmacleā€¢9mo ago
by not using your entities as DTOs create separate classes to represent your web requests and responses and map those to and from your entities
Spugnetta
SpugnettaOPā€¢9mo ago
oh yea šŸ™‚ sure dumb question sorry šŸ˜… šŸ™ well thx i figured it out šŸ™‚
Want results from more Discord servers?
Add your server