EntityCommandExecutionException 仅在 Azure SQL 上发生

EntityCommandExecutionException happening only on AzureSQL

我有以下 Class 和 Controller

    public class FieldHelper
    {
        public FieldHelper(RoomMeta rm, string typeClass)
        {
            this.typeClass = typeClass;
            this.Name = rm.Name;
            if (rm.Required)
                this.required = "required";
            else
                this.required = "optional";
        }

        public FieldHelper(EventTypeMeta etm, string typeClass)
        {
            this.typeClass = typeClass;
            this.Name = etm.Name;
            if (etm.Required)
                this.required = "required";
            else
                this.required = "optional";
        }

        public string typeClass { get; set; }

        public string Name { get; set; }

        public string required { get; set; }
    }


    [HttpGet]
    public JsonResult GetDefaultFields(int eventTypeID, int roomID)
    {
        using (var db = new MyDbContext())
        {
            List<FieldHelper> fields = new List<FieldHelper>();
            foreach(RoomMeta rm in db.RoomMetaSet.Where(rm => rm.RoomId == roomID))
            {
                fields.Add(new FieldHelper(rm, rm.FieldTypes.Name)); //Here the Exception gets thrown
            }
            foreach(EventTypeMeta etm in db.EventTypeMetaSet.Where(etm => etm.EventTypeId == eventTypeID))
            {
                fields.Add(new FieldHelper(etm, etm.FieldTypes.Name));
            }
            return Json(fields, JsonRequestBehavior.AllowGet);
        }
    }

我的数据库布局如下所示:

现在,当我 运行 在本地计算机上使用 SQL Server Express 2014 安装时,一切都按我预期的方式运行。但是,一旦我将应用程序部署到具有 Azure SQL 数据库的 Windows Azure 网站,我就会在标记的行处收到 EntityCommandExecutionException。内部异常告诉我 "There is already an open DataReader associated with this Command which must be closed first." 这似乎对我更有用,但我仍然无法弄清楚为什么这在本地而不是在线上有效。

如有任何想法,我们将不胜感激。

看来我自己找到了答案。由于我没有在查询中包含相关实体,entity framework 尝试打开另一个连接,但由于我的天蓝色 sql 数据库的限制而失败。

我改了

        foreach(RoomMeta rm in db.RoomMetaSet.Where(rm => rm.RoomId == roomID))

        foreach(RoomMeta rm in db.RoomMetaSet.Include("FieldTypes").Where(rm => rm.RoomId == roomID))

现在一切正常。