日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

EF關(guān)于報(bào)錯(cuò)Self referencing loop detected with type的原因以及解決辦法

作者:xuexixuexien 更新時(shí)間: 2022-01-17 編程語(yǔ)言

EF關(guān)于報(bào)錯(cuò)Self referencing loop detected with type的原因以及解決辦法
1)具體報(bào)錯(cuò)

{
    "Message": "出現(xiàn)錯(cuò)誤。",
    "ExceptionMessage": "“ObjectContent`1”類型未能序列化內(nèi)容類型“application/json; charset=utf-8”的響應(yīng)正文。",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": null,
    "InnerException": {
        "Message": "出現(xiàn)錯(cuò)誤。",
        "ExceptionMessage": "Self referencing loop detected for property 'auth_role' with type 'System.Data.Entity.DynamicProxies.auth_role_D04219A66C0CD8B839EF745B94309FF128B7C1D40C06E5BBBD55963530DFBAD1'. Path 'Data[1].auth_users[0]'.",
        "ExceptionType": "Newtonsoft.Json.JsonSerializationException",
        "StackTrace": "   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n   在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n   在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n   在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n   在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n   在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- 引發(fā)異常的上一位置中堆棧跟蹤的末尾 ---\r\n   在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   在 System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
    }
}

2)分析可能原因

1)web api影響了返回序列化的問(wèn)題?

2)返回json重復(fù)調(diào)用問(wèn)題?

3)確定具體原因

結(jié)論:是web api自帶的return Json把模型間的導(dǎo)航屬性也算進(jìn)去了。(A是B的導(dǎo)航屬性,B也是A的導(dǎo)航屬性,所以會(huì)無(wú)限循環(huán),導(dǎo)致Json會(huì)生成無(wú)數(shù)層)

4)解決

1)修改泛型的返回值  改成標(biāo)準(zhǔn)的返回對(duì)象類(建議)

2)刪掉外鍵關(guān)聯(lián)【也就是刪除導(dǎo)航屬性】(不建議)

3)全局刪除循環(huán)引用 webapiconfig中修改如下:(高效)

config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

4)在導(dǎo)航屬性上加注解[JsonIgnore]忽略此屬性 ef采用db first的話不適用。(低效)

原文鏈接:https://blog.csdn.net/xuexixuexien/article/details/122113324

欄目分類
最近更新