C# 如何使用实体框架构建基于3个表的JSON
我试图在MVC中使用JsonResult方法显示JSON,我使用的是实体框架,但我的问题是邮递员显示了一个服务器错误: ObjectContext实例已被释放,不能再用于需要连接的操作 我使用的查询涉及3个不同的表,但是其中一个表可能检索到3个以上不同的行 这是我的代码:C# 如何使用实体框架构建基于3个表的JSON,c#,json,asp.net-mvc,entity-framework,C#,Json,Asp.net Mvc,Entity Framework,我试图在MVC中使用JsonResult方法显示JSON,我使用的是实体框架,但我的问题是邮递员显示了一个服务器错误: ObjectContext实例已被释放,不能再用于需要连接的操作 我使用的查询涉及3个不同的表,但是其中一个表可能检索到3个以上不同的行 这是我的代码: [HttpGet] [AllowAnonymous] public JsonResult RetreiveResume(int User) { using (var context =
[HttpGet]
[AllowAnonymous]
public JsonResult RetreiveResume(int User)
{
using (var context = new DexusEntities())
{
var collection = (from p in context.CND_PersonalData join
pd in context.CND_ProfessionalData on p.UserId equals pd.UserId join
ex in context.CND_ExperiencesData on p.UserId equals ex.UserId select p).ToList();
return Json(collection, JsonRequestBehavior.AllowGet);
}
}
我的代码怎么了
提前谢谢。请尝试将退货放在以下括号后:
using (var context = new DexusEntities())
{
var collection = (from p in context.CND_PersonalData join
pd in context.CND_ProfessionalData on p.UserId equals pd.UserId join
ex in context.CND_ExperiencesData on p.UserId equals ex.UserId select p).ToList();
return Json(collection, JsonRequestBehavior.AllowGet);
}
使用后移动回油管。您正在尝试返回结果之后处理上下文。有关详细信息,请查看此链接:
您正在使用asp mvc core吗?请检查此链接:您应该包括表达式返回的主要实体的子集合。和CND_ExperiencesData.Include(“collectionName”)一样,如果你分享了你的模型定义,你会更容易得到帮助。我不确定。我想是的。它说:无法将type System.Collections.Generic.Lis转换为MySolution.CND_PersonalDataI更改为List collection=new List();但它不起作用。你仍然有相同的错误还是不同的错误?您的postman http方法是否正确?您是否也可以尝试禁用延迟加载?context.Configuration.LazyLoadingEnabled=false;卡迪尔,你最后的评论非常有效!!context.Configuration.LazyLoadingEnabled=false;解决了这个问题。我还必须对代码做一些修改。
[HttpGet]
[AllowAnonymous]
public JsonResult RetreiveResume(int User)
{
var collection = new CND_PersonalData();
using (var context = new DexusEntities())
{
context.Configuration.LazyLoadingEnabled = false;
collection = (from p in context.CND_PersonalData join
pd in context.CND_ProfessionalData on p.UserId equals pd.UserId join
ex in context.CND_ExperiencesData on p.UserId equals ex.UserId select p).ToList();
}
return Json(collection, JsonRequestBehavior.AllowGet);
}