c# - `Collection was modified; enumeration operation may not execute` error while reading data -


we have web-application , uses entity framework access data (db oracle). we've moved ef6 recently. use transactions in following way:

  1. we create transaction, have entitytransaction object , entityconnection object now.
  2. several concurrent requests done db. each request runs in own thread (actually requests come client). each request own objectcontext created using same entityconnection object. requests of transaction run in parallel partially, in given moment of time there few parallel running requests.

and under these conditions strange error started occur: exception - collection modified; enumeration operation may not execute.

it happens when data read or updated, , when connection property of entitytransaction accessed.

the beginning of stack trace may vary, end same:

system.invalidoperationexception: collection modified; enumeration operation may not execute. @ system.collections.generic.list1.enumerator.movenextrare() @ system.linq.enumerable.whereselectlistiterator2.movenext() @ system.linq.enumerable.d__142.movenext() @ system.linq.enumerable.<distinctiterator>d__811.movenext() @ system.linq.enumerable.whereenumerableiterator1.movenext() @ system.collections.generic.list1..ctor(ienumerable1 collection) @ system.linq.enumerable.tolist[tsource](ienumerable1 source) @ system.data.entity.infrastructure.interception.dbinterceptioncontext..ctor(ienumerable`1 copyfrom) @ system.data.entity.core.entityclient.entitytransaction.get_interceptioncontext()

actually there variant end of stack trace (but can see equivalent):

system.invalidoperationexception: collection modified; enumeration operation may not execute. @ system.collections.generic.list1.enumerator.movenextrare() @ system.linq.enumerable.whereselectlistiterator2.movenext() @ system.linq.enumerable.any[tsource](ienumerable1 source, func2 predicate) @ system.data.entity.infrastructure.interception.dbinterceptioncontext..ctor(ienumerable`1 copyfrom) @ system.data.entity.core.entityclient.entityconnection.get_interceptioncontext()

as can see, error occurs in dbinterceptioncontext constructor. it's kind of ef internals , collection (which modified) internal collection , has no direct link our custom code.

few notes:

  • we didn't have such error prior ef6 update.
  • error happens relatively rare, 1% of transactions.

what reason error? issue ef6? or using ef in wrong way? allowed create several objectcontext-s 1 connection supposed run concurrently?

if needed can provide examples of complete call stacks.

this problem happens if add/remove collection while iterating on it:

// throws "collection modified" error foreach (var item in mycollection) {     mycollection.remove(item); }  // fix problem, iterate on copy of collection count not change var fixedsize = mycollection.toarray(); foreach (var item in fixedsize) {     mycollection.remove(item); } 

maybe old code returned arrays, whereas ef 6 code returns ienumerable? should check create/update/delete usecases problem.


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -