Json 序列化错误 "There is already an open DataReader associated with this Command"

Error in Json Serialization "There is already an open DataReader associated with this Command"

Json 序列化命令出现此错误。我使用 Newtonsoft.Json 来避免序列化时的循环引用错误。

private IQueryable<Study> GetStudiesData()
{
    var currUser = UserManager.FindById(User.Identity.GetUserId());
    var currUserRole = currUser.Roles.First();

    IQueryable<Study> studies;
    if (User.IsInRole("SuperAdmin")) {
        studies = db.Studies; //all studies from all centers 
    }
    else {
        var assignedStudies = db.Studies.Where(s => s.AssignedUserID == currUser.Id);

        studies = db.Studies.Where(s => s.User.CenterId == currUser.CenterId && s.RoleID == currUserRole.RoleId)
            .Concat(assignedStudies);
    }

    return studies;
}

//ajax call to this function gives error
public ActionResult GetStudies(int pageSize = 10, int pageNum = 1)
{
    var studies = GetStudiesData();
    var studiesCount = studies.Count();
    var studiesPaged = studies.OrderBy(s=>s.PatientID).Skip(pageSize*pageNum).Take(pageSize);

    var result = new { TotalRows = studiesCount, Rows = studiesPaged };

    //this line gives error
    //There is already an open DataReader associated with this Command which must be closed first.
    var data = Newtonsoft.Json.JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.None,
    new Newtonsoft.Json.JsonSerializerSettings()
    {
        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    });

    return Json(data, JsonRequestBehavior.AllowGet);
}

我在 var data = Newtonsoft.Json.JsonConvert.SerializeObject(result, ...) 中尝试了 result = studiesresult = studiesPagedresult = studiesCount。 只有 studiesPaged 给出了错误。

我可以使用 MultipleActiveResultSets=True; 解决它,但是我在哪里进行第二个查询?

更新

public ActionResult GetStudies(int pageSize = 10, int pageNum = 1) { var studies = GetStudiesData().ToList(); //解决了,创建了另一个问题 //其他代码 }

现在返回的 Json 看起来像 Json 再次序列化为字符串。

"{\"TotalRows\":5,\"Rows\":[{\"AssignedUser\":null, ..... }]}"

当被 javascript 解析时,它全部拆分为一个字符数组。

更新2

已更改

//data is a json string after serialization
return Json(data, JsonRequestBehavior.AllowGet); //Serializing the Json

return Content(data, "application/json");
var assignedStudies = db.Studies.Where(s => s.AssignedUserID == currUser.Id);

studies = db.Studies.Where(s => s.User.CenterId == currUser.CenterId && 
              s.RoleID == currUserRole.RoleId).Concat(assignedStudies);

您有两个问题 DbSet<Studies> 这里

将它分配给一个列表,然后查询它应该可以防止这种情况:

public ActionResult GetStudies(int pageSize = 10, int pageNum = 1)
{
    var studies = GetStudiesData().ToList();
    var studiesCount = studies.Count();
    var studiesPaged = studies.OrderBy(s=>s.PatientID).Skip(pageSize*pageNum).Take(pageSize);

    var result = new { TotalRows = studiesCount, Rows = studiesPaged };

    //this line gives error
    //There is already an open DataReader associated with this Command which must be closed first.
    var data = Newtonsoft.Json.JsonConvert.SerializeObject(result, Newtonsoft.Json.Formatting.None,
    new Newtonsoft.Json.JsonSerializerSettings()
    {
        ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
    });

    return Json(data, JsonRequestBehavior.AllowGet);
}