zoukankan      html  css  js  c++  java
  • EF关于报错Self referencing loop detected with type的原因以及解决办法

    1)具体报错

    {
        "Message": "出现错误。",
        "ExceptionMessage": "“ObjectContent`1”类型未能序列化内容类型“application/json; charset=utf-8”的响应正文。",
        "ExceptionType": "System.InvalidOperationException",
        "StackTrace": null,
        "InnerException": {
            "Message": "出现错误。",
            "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)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
       在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
       在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
       在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)
       在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)
       在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
    --- 引发异常的上一位置中堆栈跟踪的末尾 ---
       在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       在 System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
        }
    }
    

    2)分析可能原因

      1)web api影响了返回序列化的问题?

      2)返回json重复调用问题?

    3)确定具体原因

       结论:是web api自带的return Json把模型间的导航属性也算进去了。(A是B的导航属性,B也是A的导航属性,所以会无限循环,导致Json会生成无数层)

    4)解决

        1)修改泛型的返回值  改成标准的返回对象类(建议)

        2)删掉外键关联【也就是删除导航属性】(不建议)

     3)全局删除循环引用 webapiconfig中修改如下:(高效)

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

     4)在导航属性上加注解[JsonIgnore]忽略此属性  ef采用db first的話不适用。(低效)

  • 相关阅读:
    刘瑜写给女儿的信:愿你慢慢长大
    Linux 程序编译过程的来龙去脉
    2018新版驾照驾照psd模板下载
    最新行驶证 驾照ps模板psd模板
    《我不是药神》票房超25亿 曝片段呈现群像式演技
    微信小程序打开手机浏览器访问置顶网页
    微信如何打开手机浏览器
    微信发一个网址打开后自动调用手机自带默认浏览器或提示选择浏览器打开如何实现?
    网页跳转微信关注页面
    网页跳转微信添加页面
  • 原文地址:https://www.cnblogs.com/songjl/p/9603830.html
Copyright © 2011-2022 走看看