Entity Frameworks, WebApi, JSON.Net and Serializing Circular References

While Entity Frameworks handles circular references between models very well, putting them through a WebApi web service and serializing them, Json.Net starts throwing errors.

The quickest way around this is to rebuild the objects and miss out the references from the child back to the parent.

public virtual HttpResponseMessage Get()
var data = ADRepository.GetAllocatedApplications();

// do this to get around the bloody annoying Json Circular References Issue
List result = data.Select(x => new ActiveDirectoryGroup
Id = x.Id,
Name = x.Name,
IsDeleted = x.IsDeleted,
LastUpdated = x.LastUpdated,
LastUpdatedBy = x.LastUpdatedBy,
Applications = x.Applications.Select(y => new Application
Id = y.Id,
Name = y.Name,
CommandLine = y.CommandLine,
IsDeleted = y.IsDeleted,
LastUpdated = y.LastUpdated,
LastUpdatedBy = y.LastUpdatedBy

return Request.CreateResponse<List>(System.Net.HttpStatusCode.OK, result);


About John

Software developer, lately specialising in Full Stack Web Development using c#, ASP.Net WebAPI 2, and Angular.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s