【问题标题】:How do I avoid a circular reference while serializing Entity Framework class序列化实体框架类时如何避免循环引用
【发布时间】:2011-05-25 02:14:41
【问题描述】:

我有一个使用 Entity Framework 4 的 MVC-3 (RC1) 应用程序。

我希望从控制器操作中返回一个 JSON 对象。该对象被其他对象引用,显然返回引用。

因此我收到以下循环引用错误:

“/”应用程序中的服务器错误。

检测到循环引用 在序列化类型对象时 'Application.Models.ReferenceObject'。

描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。

异常详情: System.InvalidOperationException:一个 在检测到循环引用时 序列化一个类型的对象 'Application.Models.ReferenceObject'。

注意:ApplicationReferenceObject 显然是实际命名空间/对象的替代品。

根据Stack Overflow: Circular reference exception when serializing LINQ to SQL classes,这可以使用 JSON.Net 来克服;但是我想避免这种情况,而是尝试从被序列化的对象中排除有问题的引用属性。

什么意思?

我想做这样的事情:

IList<ReferenceObject> list = Repository.GetReferenceObjects();
return Json(list.**<method>**("ObjectsReferencingThis"));

其中**&lt;method&gt;** 是与ObjectQuery(Of T).Include 方法相反的方法,ObjectsReferencingThis 是导致循环引用的属性。

注意:我不希望删除这些属性或创建 POCO,因为这只会影响 Json 序列化。

有人可以帮忙吗?

:)

【问题讨论】:

    标签: entity-framework-4 asp.net-mvc-3 objectquery


    【解决方案1】:

    在处理我之前的一个项目时,我遇到了类似的问题。 这是我最终做的:

    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 的结果如下所示:

    {
    "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 对象中:)

    【讨论】:

    • 嗨@laurvasile,是的,这也是我用作“解决方法”的方法。我对它不是 100% 满意,因为它不是完全动态的……对模型的任何更改都需要反映到这个手动 JSON 转换中。我认为如果你可以排除一个字段/属性/引用作为 LINQ 查询的一部分,它会更有效,类似于 INCLUDE 函数。
    • 被接受为答案(尽管它实际上是一种“解决方法”恕我直言)
    【解决方案2】:

    我做了一个非常简单的解决方案,如果您的列表很大,不推荐使用它

    letters=UserOperations.GetDepartmentLettersForSecretary(pageNumber, pageSize,(Session["User"] as User).DepartmentID.Value, (Session["User"] as User).ID);
    
    foreach (Letter letter in letters)
    {
        letter.LetterStatus.Letters = null;
    }
    

    在我的情况下circular reference 的问题在于 LetterStatus.Letters 所以我Iterated through the listassigned it to null

    正如我告诉你的那样,如果你有very big list,它就是not recommended

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2019-08-09
      • 2012-04-21
      相关资源
      最近更新 更多