如何删除在实体框架循环引用? [英] How to remove circular reference in Entity Framework?

查看:204
本文介绍了如何删除在实体框架循环引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户和订单实体之间的循环引用导致序列化过程中的异常。有没有什么办法来强制EF生成这两个实体之间的一个方向的参考?在此先感谢!

The circular reference between my Customer and Order entities caused a exception during serialization. Is there any way to force EF to generate one-direction reference between these two entities? Thanks in advance!

推荐答案

当我需要序列化,我一般投射到其他类型。这消除了循环引用,再加上其他的数据我不想连载。例如:

When I need to serialize, I generally project onto other types. This eliminates circular references, plus other data I don't want serialize. For example:

var q = (from c in Repository.Customers()
         where c.Id == id
         select new 
         {
             Name = c.Name,
             Orders = from o in C.Orders
                      select new
                      {
                          Date = o.Date
                      }
         }).First();
return Json(q);

这篇关于如何删除在实体框架循环引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆