got a circular reference while serializing using Controller.Json() method
I have a model edmx model and am using POCO like this:
public class ContactPerson
{
public virtual Guid ContactPersonId { get; set; }
public virtual string FullName { get; set; }
public virtual Guid PositionId { get; set; }
public virtual DateTime Birthday { get; set; }
public virtual Position Position { get; set; }
}
public class Position
{
public virtual Guid PositionId { get; set; }
public virtual string Name { get; set; }
public virtual ICollection<ContactPerson> ContactPersons { get; set; }
}
When I try to seriali开发者_如何学运维ze from a in _context.Positions select a
using Json() method, it detects a circular reference. I belive this happens because Position have a reference to ContactPersons collection and ContactPerson at same time have a reference to Position.
The question is obvious, what to do?
Try to use ScriptIgnoreAttribute at ContactPerson.Position property. It will say JavaScriptSerializer not to serialize that property and it will break circular reference.
精彩评论