JsonException: A possible object cycle was detected
System.Text.Json found a reference loop (e.g. Order → Customer → Orders → …) while serialising, usually from EF Core navigation properties.
Seen on:
.NET
Meaning
Returning EF entities directly from APIs serialises navigation properties in both directions, creating cycles (or depth > 64).
Common causes
- Returning EF Core entities with bidirectional navigation properties
- Include() loading both sides of a relationship
- Self-referencing hierarchies (parent/children)
⚡ Quick fix
- Return DTOs/records with only the fields the client needs
- Or configure
ReferenceHandler.IgnoreCycles - Avoid Include() of back-references
Detailed fix by platform
.NET
- Ignore cycles globally (quick fix):csharp
builder.Services.AddControllers().AddJsonOptions(o => o.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles);
Code examples
Better: project to a DTO
csharp
var orders = await db.Orders
.Select(o => new OrderDto(o.Id, o.Total, o.Customer.Name))
.ToListAsync();How to diagnose
- Type — Which entity graph is serialised?
- Navigation — Which properties point back?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026