## Self referencing loop detected for property `xxx`
It's very common to have circular reference in models. For example, the following models shows a bidirection navigation property:
```C#
public class PlanCoverageNetwork : Entity
{
[CanBeNull]
public virtual ICollection<PlanCoverageNetworkException> Exceptions { get; set; }
}
public class PlanCoverageNetworkException : Entity
{
[Required]
[Index("IX_UNQ_PCNE", 1, IsUnique = true)]
[ForeignKey("PlanCoverageNetwork")]
public int PlanCoverageNetworkId { get; set; }
/* needed for the foreign key relationship stated above */
public virtual PlanCoverageNetwork PlanCoverageNetwork { get; set; }
}
```
The error occurs because the serializer doesn't know how to handle cirular reference. (Similar error occurs in xml serializer as well)
### Fix 1: Ignore and preserve reference attributes
This fix is decorate attributes on model class to control the serialization behavior on model or property level. To ignore the property:
```C#
public class PlanCoverageNetwork : Entity
{
[CanBeNull]
[JsonIgnore] // + for JSON.NET
[IgnoreDataMember] // + for XmlDCSerializer
public virtual ICollection<PlanCoverageNetworkException> Exceptions { get; set; }
}
```
https://code.msdn.microsoft.com/Loop-Reference-handling-in-caaffaf7
### Fix 2 (JSON.NET): Pass setting option parameter as follows:
```C#
var networkListJson = JsonConvert.SerializeObject(
totalPlanCoverageNetworkList,
Formatting.None,
new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}
);
```
https://stackoverflow.com/questions/13510204/json-net-self-referencing-loop-detected