当使用 Using 语句时收到此错误
The operation cannot be completed because the DbContext has been disposed.
And While Use Without Using Not 收到错误 出现这个错误的原因是什么?
using (var context = new ProCamContext())
{
var q = context.CatCamera.Where(p => p.Category.Equals(value)).Select(p => p.CatDetails);
return Json(q);
}
最佳答案
您的问题无疑是由于延迟执行引起的。
强制该查询评估应该工作,方法是调用一个强制它访问数据库的方法。 ToList
应该足够了:
var q = context.CatCamera
.Where(p => p.Category.Equals(value))
.Select(p => p.CatDetails).ToList();
目前,在评估您的查询时,您的上下文将被处理掉并且无法调入数据库。因此错误。
关于c# - 使用语句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18180036/