ef entity转json引起的Self referencing loop
问题简介:前段时间做项目时,将取到的entity往Redis cache里存放时报多重引用的错误。
Self referencing loop detected for property 'CheckItemCategory' with type。
一、问题详情
1、chectItemCategory与CheckItem实体介绍
2、我们用ef取了List<CheckItemCategory>,以Json的形式存放到redis中。但在JsonConvert.SerializeObject时报多重引用错误。
3、细追原因,NewtonSoft.Json 对Entity(CheckItemCategory)序列化时,会遍历CheckItemCategory中的所有属性进行转换。当遇到导航属性CheckItems时,会向数据库继续请求checkItems数据(ef延迟加载)。而在转换checkItem实体时,checkItem实体里面又有CheckItemCategory导航属性。CheckItemCategory与checkItem互相引用,多重引用死循环问题。
二、解决方案
1、方法1。推荐用这个。
在entity的checkItems导航属性上加[JsonIgnore]特性来忽略该属性。
关于newtonsoft.json高级用法参见 http://www.cnblogs.com/yanweidie/p/4605212.html
2、方法2。删除CheckItemCategory类中的checkItems属性。但是此方法治标不治本,(1)本来ef配置两者外键关系时需要用
(2)在具体应用时可能已经在用他们的关系延迟加载,进行业务操作。
(3)在取checkItem转json时也会将CheckItemCategory取出一并转到json中去,比较坑。
三、思维延伸
在用webapi返回结果时,千万不要直接返回entity,因为也会把导航属性全部取出。暴露了其他数据信息。
ef entity转json引起的Self referencing loop的更多相关文章
- JSON.NET的Self referencing loop detected with type的原因以及解决办法
模型中有循环引用是很常见的.例如,以下模型显示双向导航属性: : public class Category : { : public Category() : { : Products = new ...
- .NET JSON 转换 Error ” Self referencing loop detected for type“
在进行实体转换为Json格式报错如下图: Self referencing loop detected for property 'md_agent' with type 'System.Data.E ...
- EF关于报错Self referencing loop detected with type的原因以及解决办法
1)具体报错 { "Message": "出现错误.", "ExceptionMessage": "“ObjectContent` ...
- EF(Entity Framework)多对多关系下用LINQ实现"NOT IN"查询
这是今天在实际开发中遇到的一个问题,需求是查询未分类的博文列表(未加入任何分类的博文),之前是通过存储过程实现的,今天用EF实现了,在这篇博文中记录一下. 博文的实体类BlogPost是这样定义的: ...
- c#使用 Newtonsoft.Json 将entity转json时,忽略为null的属性
c#使用 Newtonsoft.Json 将entity转json时,忽略为null的属性,直接在属性上加下面的特性 [JsonProperty(NullValueHandling=NullValue ...
- c# json 序列化时遇到错误 error Self referencing loop detected for type
参考网址:http://blog.csdn.net/adenfeng/article/details/41622255 在写redis缓存帮助类的时候遇到的这个问题,本来打算先序列化一个实体为json ...
- Json Self referencing loop detected
Self referencing loop detected......的错误 解决方案: 1 增加 [JsonIgnore] 过滤关联,使其不参与序列化. 这个方法简单粗暴.但是你就没办法获取关 ...
- [转载]灵动思绪EF(Entity FrameWork)
很久之前就想写这篇文章了,但是由于种种原因,没有将自己学习的EF知识整理成一片文章.今天我就用CodeFirst和ModelFirst两种方式的简单案例将自己学习的EF知识做个总结. 在讲解EF之前, ...
- EF(Entity Framework)通用DBHelper通用类,增删改查以及列表
其中 通用类名:DBhelper 实体类:UserInfo 1 //新增 2 DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo& ...
随机推荐
- 单调性 [1 + 1 / (n)]^n
def f(n): n += 0.0 s = 1 + 1 / (n) r = pow(s, n) print(n, ',', r) return r l = []for i in range(1, 1 ...
- 流计算技术实战 - CEP
CEP,Complex event processing Wiki定义 "Complex event processing, or CEP, is event processing that ...
- Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
kafka 0.10.0.0 released Interceptors的概念应该来自flume 参考,http://blog.csdn.net/xiao_jun_0820/article/det ...
- LeetCode 104 Maximum Depth of Binary Tree 解题报告
题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- mysql windows开启客户端连接权限
use mysql; select 'host' from user where user='root'; update user set host = '%' where user ='root ...
- Python开发【笔记】:获取目录下所有文件
获取文件 import os def sub_dirs(rdir): li = os.listdir(rdir) return li def main(rdir): content = sub_dir ...
- 【三分】light bulb(zoj3203)
题目描述: 如图,你可以在房间里移动,灯泡的高度为H,你的身高为h,灯泡与墙的水平距离为D,求你影子的最长长度(影子长度=地上影子长度+墙上影子长度) 样例输入: 0.5 样例输出: 1.000 0. ...
- Spring加载classpath与classpath*的过程剖析
使用spring-boot 1.5.7 在resource目录下创建i18n文件夹 使用spring的默认配置没有加载到文件 # INTERNATIONALIZATION (MessageSource ...
- mysql 查询优化杂谈
一.把某些判断移动到应用层 我们需要在一张表里面删除某种类型的数据,大概的表结构类似这样: CREATE TABLE t ( id INT, tp ENUM ("t1", &quo ...
- Linux系统上传文件与下载文件命令
我们用的服务器都是Linux系统的,如果用的是远程服务器,就需要将我们的代码推送过去,这里可以用到PSCP命令. (一)上传 pscp 本机文件的路径以及文件名 远程主机的用户名@远程主机IP:想要存 ...