带有MVC3和EF4 CTP5w的JSON序列化的循环引用异常

nak*_*hak 13 c# asp.net entity-framework-4 entity-framework-ctp5 asp.net-mvc-3

当我尝试序列化通过EF4 CTP5返回的对象时,我遇到循环引用问题.我使用代码第一种方法和简单的poco为我的模型.

我已经将[ScriptIgnore]属性添加到任何提供对对象的反向引用的属性,并且令人烦恼的是,如果我手动实例化poco,它们似乎工作正常,即它们串行化为JSON,并且确认了scriptignore属性.但是,当我尝试序列化从DAL返回的对象时,我得到循环引用异常"序列化'System.Data.Entity.DynamicProxies.xxxx'类型的对象时检测到循环引用"

我已经尝试了几种方法来检索数据,但他们都遇到了这个错误:

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

下面的方法效果稍好,而不是时间段动态代理对象导致循环引用其为约会对象.

    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);
    }
Run Code Online (Sandbox Code Playgroud)

这个问题的任何想法或解决方案?

更新 我希望尽可能使用开箱即用的序列化器,尽管Json.Net通过nuget可以替代我希望它可以像我预期的那样使用它...

JMo*_*gan 4

我在 IIS 托管的 WCF 服务上遇到了类似的问题,并尝试使用 DataContractJsonSerializer 类序列化 POCO 对象。内置的 JSON 序列化器似乎根本不处理循环引用。我可以通过使用JSON.net序列化器自己处理序列化来解决这个问题,并且只从我的方法中返回 json 字符串。JSON.net 序列化器有一个选项可以忽略循环引用,因为 json 本身不支持它们。