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)\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--- 引发异常的上一位置中堆栈跟踪的末尾 ---\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影响了返回序列化的问题?

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的話不适用。(低效)

EF关于报错Self referencing loop detected with type的原因以及解决办法的更多相关文章

  1. JSON.NET的Self referencing loop detected with type的原因以及解决办法

    模型中有循环引用是很常见的.例如,以下模型显示双向导航属性: : public class Category : { : public Category() : { : Products = new ...

  2. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  3. Self referencing loop detected with type

    json.net namespace EFDAL{    using System;    using System.Collections.Generic;    using Newtonsoft. ...

  4. c# json 序列化时遇到错误 error Self referencing loop detected for type

    参考网址:http://blog.csdn.net/adenfeng/article/details/41622255 在写redis缓存帮助类的时候遇到的这个问题,本来打算先序列化一个实体为json ...

  5. .NET JSON 转换 Error ” Self referencing loop detected for type“

    在进行实体转换为Json格式报错如下图: Self referencing loop detected for property 'md_agent' with type 'System.Data.E ...

  6. 报错:“不是有效的Win32应用程序”的解决办法

    Win7.Win8下用VS2013编译完的程序,拿到32位WindowsXP虚拟机下运行有时候会报错:

  7. linux centos环境下,perl使用DBD::Oracle遇到报错Can't locate DBD/Oracle.pm in @INC 的解决办法

    前言 接手前辈的项目,没有接触.安装.使用过perl和DBD::Oracle,也没有相关的文档记录,茫茫然不知所措~~.一开始发现这个问题,就想着迅速解决,就直接在google上搜报错信息,搜索的过程 ...

  8. mysql5.7.18.1修改用户密码报错ERROR 1054 (42S22): Unknown column 'password' in 'field list'解决办法

    本意向修改一个用户的密码,网上搜到的命令为如下 mysql> update user set password=password(“新密码”) where user=”用户名”; 执行后报错 E ...

  9. Oracle 插入时间时 报错:ORA-01861: 文字与格式字符串不匹配 的解决办法

    一.写sql的方式插入到Oracle中 往oracle中插入时间  '2007-12-28 10:07:24'如果直接按照字符串方式,或者,直接使用to_date('2007-12-28 10:07: ...

随机推荐

  1. SourceTree切换语言

    点击ok,重启SourceTree即生效 over!over!over!

  2. unity2d 动画

    1 资源test.jpg(如下)放入Resources文件夹 2 切割图片 点击图片,在inspector中,选择Texture Type为Sprite(2D and UI),然后点击Sprite E ...

  3. SpringBoot2.0 整合 Swagger2 ,构建接口管理界面

    一.Swagger2简介 1.Swagger2优点 整合到Spring Boot中,构建强大RESTful API文档.省去接口文档管理工作,修改代码,自动更新,Swagger2也提供了强大的页面测试 ...

  4. 在maven中引入本地jar包的方法

    一.第一种方式: 1.电脑安装maven 2.下载jar.例如 gj.jar 3.把jar随便放一个位置 4.在jar包目录下打开cmd输入: mvn install:install-file -Df ...

  5. C 语言实例 - 矩阵转换

    C 语言实例 - 矩阵转换 C 语言实例 C 语言实例 矩阵转换. 实例 #include <stdio.h> int main() { ][], transpose[][], r, c, ...

  6. docker+jenkins实现持续集成

    持续集成的概念我就不多说了,网上一大堆,今日主要讲讲怎么通过docker 和jenkins实现项目的持续集成.看着简单,网上完整教程很少,本人摸索很久才搞定!!! 一.首先安装docker.安装过程可 ...

  7. Python及bs4、lxml、numpy模块包的安装

    http://blog.csdn.net/tiantiancsdn/article/details/51046490(转载) Python及bs4.lxml.numpy模块包的安装 Python 的安 ...

  8. Flask (一) 简介

    Flask简介 Flask是一个基于Python实现的Web开发‘微’框架 'MicroFramework' Django是一个重型框架 官方文档: http://flask.pocoo.org/do ...

  9. SSAS 维度属性自定义排序

    默认设置: 排序设置:

  10. CF1072C Cram Time

    思路: 首先二分找到使x * (x + 1) / 2 <= a + b最大的x,然后令p = min(a, b), q = max(a, b),按照x,x - 1, ..., 1的顺序选取数字把 ...