วันพฤหัสบดีที่ 26 มิถุนายน พ.ศ. 2557

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.xxx'

This happened when i need to use auto complete to get Object list by Entity.
Yep i support i use Lazy loading so
before call and return code must config for not Lazy loading like


db.Configuration.ProxyCreationEnabled = false;

1 ความคิดเห็น:

Unknown กล่าวว่า...

I know this is a bit late, but maybe it will help someone else. For JSON, flatten and pay attention to any navigation properties (assuming an ORM like EntityFramework)...

In your case--the Destination navigation property... If it's a navigation property, you need to pull out the information (flatten)--e.g., Destination.DestinationName

public JsonResult getJson()
{
return this.Json(
new {
Result = (from obj in db.Product select new {Id = obj.Id, Name = obj.Name, obj.Category.Description})
}
, JsonRequestBehavior.AllowGet
);
}

Go take a look at the navigation property for Category... Chances are that you might have something similar to obj.Category, when you may actually intended to have obj.Category.Description or something similar (e.g., obj.Destination.DestinationName)... Navigation properties usually reference back to the parent again. JSON serializers don't do so well with understanding those "circular" references.

;-) I had the same issue a while back and I beat my head on the table when I realized what I had done.