A circular reference was detected while serializing an object of type 'SubSonic.Schema .DatabaseColumn'.

Jon picture Jon · Jul 20, 2009 · Viewed 215.7k times · Source

I am trying to do a simple JSON return but I am having issues I have the following below.

public JsonResult GetEventData()
{
    var data = Event.Find(x => x.ID != 0);
    return Json(data);
}

I get a HTTP 500 with the exception as shown in the title of this question. I also tried

var data = Event.All().ToList()

That gave the same problem.

Is this a bug or my implementation?

Answer

Darin Dimitrov picture Darin Dimitrov · Jul 20, 2009

It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer. Do you need all the columns? You could pick up only the properties you need in the view:

return Json(new 
{  
    PropertyINeed1 = data.PropertyINeed1,
    PropertyINeed2 = data.PropertyINeed2
});

This will make your JSON object lighter and easier to understand. If you have many properties, AutoMapper could be used to automatically map between DTO objects and View objects.