markdown #C模型相关的例外

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown #C模型相关的例外相关的知识,希望对你有一定的参考价值。

## 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

以上是关于markdown #C模型相关的例外的主要内容,如果未能解决你的问题,请参考以下文章

markdown [例外] #exception #redis

markdown 例外

IC基础:时序分析过程需要的相关计算以及处理方法

IC基础:时序分析过程需要的相关计算以及处理方法

c/c++ | int * 例外,int 给定(返回一个指针??)

c_cpp 自定义例外