我正在使用实体框架,偶尔我会得到这个错误.
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为什么不重用连接?