使用JSON序列化与MVC3和EF4 CTP5w循环引用除外 [英] Circular Reference exception with JSON Serialisation with MVC3 and EF4 CTP5w

查看:122
本文介绍了使用JSON序列化与MVC3和EF4 CTP5w循环引用除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环引用问题,当我试图通过连载EF4 CTP5返回的对象。即时通讯使用我的模型code第一种方法和简单的POCO的。

I'm having problems with a circular reference when i try and serialise an object returned via EF4 CTP5. Im using the code first approach and simple poco's for my model.

我已经添加[ScriptIgnore]属性的一个对象提供一个反向引用和烦人的每一个似乎很好地工作,如果我手动实例化它们序列化到JSON的POCO的,即罚款和scriptignore属性被确认的任何属性。然而,当我尝试连载从DAL我得到循环引用异常返回的对象,而序列化类型的对象System.Data.Entity.DynamicProxies.xxxx'中检测到循环引用

I have added [ScriptIgnore] attributes to any properties that provide a back references to an object and annoyingly every seems to work fine if i manually instantiate the poco's, i.e. they serialise to JSON fine, and the scriptignore attribute is acknowledged. However when i try and serialise an object returned from the DAL i get the circular reference exception "A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.xxxx'"

我试图检索数据,多种方法,但他们都被卡住,出现此错误:

I have tried several ways of retreiving the data but they all get stuck with this error:

    public JsonResult GetTimeSlot(int id) {
        TimeSlotDao tsDao = new TimeSlotDao();
        TimeSlot ts = tsDao.GetById(id);
        return Json(ts);
    }

下面的方法效果稍微好一点的,而不是导致循环REFFERENCE其任命对象的动态时隙代理对象。

The method below works slightly better as rather than the timeslot dynamic proxied object causing the circular refference its the appointment object.

    public JsonResult GetTimeSlot(int id) {
        TimeSlotDao tsDao = new TimeSlotDao();
            var ts = from t in tsDao.GetQueryable()
                 where t.Id == id
                 select new {t.Id, t.StartTime, t.Available, t.Appointment};
        return Json(ts);
    }

任何意见或解决这个问题?

Any ideas or solutions to this problem?

更新
我想preFER通过的NuGet使用开箱串行器如果可能的话,虽然Json.Net是确定作为一种替代我希望它可以使用它,因为我打算,以及...

Update I would prefer to use the out of the box serialiser if possible although Json.Net via nuget is ok as an alternative i would hope its possible to use it as I intended as well...

推荐答案

我有一个类似的问题与IIS托管WCF服务,并试图序列化POCO与DataContractJsonSerializer类对象。内置的JSON序列化似乎并没有在所有处理循环引用。我可以通过使用 JSON.net 串行处理系列化自己绕过它,只是从我的方法返回JSON字符串。该JSON.net串行器有一个选项忽略循环引用的JSON本身不支持他们。

I had a similar problem with an IIS hosted WCF service and trying to serialize POCO objects with the DataContractJsonSerializer class. The built in JSON serializer does not seem to handle circular references at all. I was able to get around it by handling the serialization myself using the JSON.net serializer, and just returning json strings from my methods. The JSON.net serializer has an option to ignore circular references as json itself does not support them.

这篇关于使用JSON序列化与MVC3和EF4 CTP5w循环引用除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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