【发布时间】:2017-04-13 15:36:55
【问题描述】:
我在使用 Linq 获取异步结果时遇到问题。 每次我收到错误“ObjectContext 实例已被释放,不能再用于需要连接的操作”时。 - 但我只在我的本地数据库上得到它,当我在我们的测试数据库上尝试代码时,一切正常......
我的代码:
var manufacturerData= await _manufacturerDataService.GetAllAsync();
return Json(
manufacturerData.Select(x => new
{
x.Id,
ManufacturerName = x.Manufacturer.Name,
CountryName = x.Country.Name,
x.ProductType,
ContractTypes = x.ContractTypes != null ? string.Join(" ", x.Types.Select(c => c.Name)) : ""
}), JsonRequestBehavior.AllowGet);
我在本地机器上有一个测试数据库的备份,所以我不明白为什么它不起作用。
【问题讨论】:
-
GetAllAsync是否返回IQueryable?如果是这样,请在Select末尾添加ToList以确保评估 EF 查询。 -
GetAllAsync正在返回一个IEnumerable。我尝试添加ToList,但现在我得到了The object reference was not set to an object instance.. 这太疯狂了
标签: c# sql-server linq objectcontext