问题描述
我使用实体框架4的MVC-3(RC1)应用程序。
I have an MVC-3 (RC1) application using Entity Framework 4.
我想从一个控制器动作返回一个JSON对象。这个目的是通过其他的目的,这显然返回参考引用
I wish to return a JSON object from a controller action. This object is referenced by other objects, which obviously return the reference.
我因此收到以下循环引用错误:
I thus receive the following circular reference error:
中的服务器错误'/'应用。
中检测到循环引用
而序列化类型的对象
Application.Models.ReferenceObject。
A circular reference was detected while serializing an object of type 'Application.Models.ReferenceObject'.
说明:未处理的异常
的执行过程中发生
当前Web请求。请检查
堆栈跟踪有关的详细信息
该错误以及它起源于
在code。
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
异常详细信息:
System.InvalidOperationException:一
而检测到循环引用
序列类型的对象
Application.Models.ReferenceObject。
Exception Details: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'Application.Models.ReferenceObject'.
注:应用&安培; ReferenceObject 显然替代实际的命名空间/对象。
NB: Application & ReferenceObject are obviously replacements for the actual namespace / object.
据堆栈溢出:LINQ序列化时,循环引用例外SQL类的,这可以通过使用JSON.Net加以克服;不过,我想避免这种情况,而是试图排除对象被序列化违规的参考属性。
According to Stack Overflow: Circular reference exception when serializing LINQ to SQL classes, this can be overcome using JSON.Net; however I would like to avoid this and instead try to exclude the offending reference properties from the object being serialized.
这是什么意思?
我想要做这样的事情:
IList<ReferenceObject> list = Repository.GetReferenceObjects();
return Json(list.**<method>**("ObjectsReferencingThis"));
其中, **&LT;方法&gt; **
一些方法,做相反的的 的ObjectQuery(Of T)已.INCLUDE
方法和 ObjectsReferencingThis
是导致循环引用的属性。
where **<method>**
is some method that does the opposite to the ObjectQuery(Of T).Include
method and ObjectsReferencingThis
is the property that is causing the circular reference.
注:我不希望删除这些属性或创建作为波苏斯这只会影响JSON序列
NB: I do not wish to remove these properties or create POCOs as this only affects the Json serialization.
任何人能帮助吗?
:)
推荐答案
我的时候对我的previous项目之一,曾遇到过类似问题。
以下是我落得这样做:
I had a similar problem when worked on one of my previous project.Here is what I ended up doing:
IList<Product> list = Repository.GetProducts();
var collection = products.Select(product => new
{
id = product.Id,
name = product.Name,
detailUrl = product.DetailUrl,
imageLargeUrl = product.ThumbNailUrl,
tagtitle = product.Name.ToUpper(),
tagheader = "Words our cherished patrons use to describe this product",
tagwords = from tag in product.Tags group tag by tag.Name into words select new { name = words.Key, weight = words.Count() }
});
var result = new {id = inquiry.Id, products = collection, };
return this.Jsonp(result);
下面是结果的Json会是什么样子:
Here is how the result Json would look like:
{
"id" : 2,
"products" : [{
"id" : "3605970008857",
"name" : "TITLE1",
"detailUrl" : "http://www.urlhere.com",
"tagwords" : [{
"name" : "roses",
"weight" : 1
},
{
"name" : "cotton",
"weight" : 1
},
{
"name" : "happy",
"weight" : 1
}]
},
{
"id" : "3605970019891",
"name" : "TITLE2",
"detailUrl" : "http://www.urlhere.com",
"tagwords" : []
}],
}
您也可以从你引用的对象将结果添加任何其他属性,如你所愿,在你的JSON对象中显示:)
You can also add any other properties from you referenced objects to the result as you wish,to be shown in your Json object :)
这篇关于如何避免循环引用,而序列化实体框架类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!