我有一个n层asp.net应用程序,从我DAL返回一个对象到BAL像这样:的.Net实体ObjectContext的线程错误
public IEnumerable<SourceKey> Get(SourceKey sk)
{
var query = from SourceKey in _dataContext.SourceKeys
select SourceKey;
if (sk.sourceKey1 != null)
{
query = from SourceKey in query
where SourceKey.sourceKey1 == sk.sourceKey1
select SourceKey;
}
return query.AsEnumerable();
}
这个结果经过我的业务层和打UI层显示给最终用户。我不懒惰加载,以防止我的应用程序的其他层中的查询执行。
我在DAL删除对象创建的另一个功能:
public void Delete(SourceKey sk)
{
try
{
_dataContext.DeleteObject(sk);
_dataContext.SaveChanges();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " " + ex.StackTrace + " " + ex.InnerException);
}
}
当我尝试调用调用“获取”功能后,“删除”,我收到此错误:
New transaction is not allowed because there are other threads running in the session
这是一个ASP.Net应用程序。我的DAL包含一个实体数据模型。我有上述函数的类共享相同的_dataContext,它在我的构造函数中实例化。我的猜测是,读者仍然从“获取”功能打开,并没有关闭。我怎样才能关闭它?
在我的UI层,我遍历结果并试图删除它们像这样:
foreach(SourceKey sk in skm.Get(new SourceKey()))
{
Debug.WriteLine(sk.sourceKey1);
skm.Delete(sk);
}
编辑
的错误是在这条线存在的:
_dataContext.DeleteObject(sk);
这是我的理解是查询将在AsEnumerable()上执行。我确实从我的foreach循环中删除了它,并且它工作正常 – 2010-05-24 14:51:40