I am using Entity Framework and occasionally i will get this error.
EntityCommandExecutionException
{"There is already an open DataReader associated with this Command which must be closed first."}
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands...
Even though i am not doing any manual connection management.
this error happens intermittently.
code that triggers the error (shortened for ease of reading):
if (critera.FromDate > x) {
t= _tEntitites.T.Where(predicate).ToList();
}
else {
t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList());
}
using Dispose pattern in order to open new connection every time.
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());
}
}
still problematic
why wouldn't EF reuse a connection if it is already open.
predicate
andhistoricPredicate
variables are. I've discovered that if you passFunc<T, bool>
toWhere()
it will compile and sometimes work (because it does the "where" in memory). What you should be doing is passingExpression<Func<T, bool>>
toWhere()
. – James Mar 22 '17 at 13:48