我面临着异常ObjectContext 实例已被释放,不能再用于需要连接的操作即使在使用 Include 方法之后也是如此。
这里是检索实体的函数:
public List<Entity.CapacityGrid> SelectByFormula(string strFormula, int iVersionId)
{
// declaration
List<Entity.CapacityGrid> oList;
// retrieve ingredients
oList = (from Grid in _Dc.CapacityGrid.Include("EquipmentSection")
join Header in _Dc.CapacityHeader
on Grid.HeaderId equals Header.HeaderId
where Header.Formula == strFormula
&& Header.VersionId == iVersionId
select Grid).ToList();
// return
return oList;
这里是该函数的用法:
// retrieve ingredient quantity by equipement
using (Model.CapacityGrid oModel = new Model.CapacityGrid(Configuration.RemoteDatabase))
oQuantity = oModel.SelectByFormula(strFormulaName, iVersionId);
// code to throw the exception
var o = (oQuantity[0].EquipmentSection.TypeId);
据我所知,使用正在关闭连接。我认为 ToList() 会在关闭之前实例化包含中的对象列表和相关对象。
有人可以指出我做错了什么吗?
抱歉,我的问题不清楚。我确实明白,在 using 的括号内包含抛出异常的行是有效的,但我不明白为什么包含不起作用?
谢谢你!
尝试改变
// retrieve ingredient quantity by equipement
using (Model.CapacityGrid oModel = new Model.CapacityGrid(Configuration.RemoteDatabase))
{ // <-- **add these**
oQuantity = oModel.SelectByFormula(strFormulaName, iVersionId);
// code to throw the exception
var o = (oQuantity[0].EquipmentSection.TypeId);
} // <-- **add these**
Ref: http://msdn.microsoft.com/en-us/library/yh598w02.aspx http://msdn.microsoft.com/en-us/library/yh598w02.aspx
With no {}
为了方便使用,该连接位于第一行之后。因为实体框架使用表达式树(这意味着请求在真正需要时才会执行),所以您的查询发生在var o = (oQuantity[0].EquipmentSection.TypeId);
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)