C
C#•14mo ago
StarMe

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•14mo 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
StarMe
StarMeOP•14mo 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•14mo ago
you can just assign one side of the relationship and EF will figure it out when you call SaveChanges
StarMe
StarMeOP•14mo ago
how can i retrive this autor without getting a json circular exception for example?
No description
Jimmacle
Jimmacle•14mo 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
StarMe
StarMeOP•14mo ago
oh yea šŸ™‚ sure dumb question sorry šŸ˜… šŸ™ well thx i figured it out šŸ™‚

Did you find this page helpful?