© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
30 replies
ky1e

How to map Entity Framework entities containing circular references to DTOs?

When querying data with Entity Framework, the result data appears to contain circular references.

(Using the example data from the attached screenshots) If I query a Sport and include its Leagues, the result is a Sport with list of Leagues, where each League is associated with a Sport, and that Sport is associated with a list of Leagues... you get the idea.

With the screenshotted mapper classes, attempting to convert the Sport entity to a DTO causes the app to crash. Makes sense. If i remove one of the related DTOs from the other DTO's definition, the mapper has no problem, but this affects the flexibility I'd like to have. I would also like to avoid using AutoMapper or other third-party libraries.

What is the solution to this? Is there something I can/should do with Entity Framework to avoid circular references in the query results? Or is there a way to set up mapper classes to handle this in a way that prevents redundant data from being mapped? (e.g. do not map the Sport inside League when the League was retrieved from its Sport parent).
circular_references.png
circular_references2.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Entity Framework - Circular References
C#CC# / help
2y ago
Where to map DTOs & Entities
C#CC# / help
4y ago
✅ Circular References
C#CC# / help
2y ago
❔ Entity Framework relationship problem between entities.
C#CC# / help
4y ago