相关疑难解决方法(0)

实体框架:已经有一个与此命令关联的开放DataReader

我正在使用实体框架,偶尔我会得到这个错误.

EntityCommandExecutionException
{"There is already an open DataReader associated with this Command which must be closed first."}
   at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands...
Run Code Online (Sandbox Code Playgroud)

即使我没有做任何手动连接管理.

这个错误间歇性地发生.

触发错误的代码(为了便于阅读而缩短):

        if (critera.FromDate > x) {
            t= _tEntitites.T.Where(predicate).ToList();
        }
        else {
            t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
        }
Run Code Online (Sandbox Code Playgroud)

使用Dispose模式以便每次都打开新连接.

using (_tEntitites = new TEntities(GetEntityConnection())) {

    if (critera.FromDate > x) {
        t= _tEntitites.T.Where(predicate).ToList();
    }
    else {
        t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
    }

}
Run Code Online (Sandbox Code Playgroud)

仍有问题

如果连接已经打开,EF为什么不重用连接?

linq entity-framework sql-server-2008

279
推荐指数
6
解决办法
15万
查看次数

标签 统计

entity-framework ×1

linq ×1

sql-server-2008 ×1