Cluj München 1140km

Gânduri clujene din Bavaria

München - Munich - Monaco di Baviera

21. aprilie 2009 14:08
by skorpionking
0 Comentarii

.NET 60 seconds code: Entity Framework SaveChanges - Transaction is not allowed error

21. aprilie 2009 14:08 by skorpionking | 0 Comentarii

Doing some .NET Entity Framework queries and updates the other day, I kept getting this exception:

"New transaction is not allowed because there are other threads running in the session."

foreach (UserEntity u in Users.UserEntitySet)

      // A Users.SaveChanges(true) here will throw the error.

The reason is we have an open Reader in our foreach and we can't nest trasactions like that.  The solution is to read everything we need first so we close the reader.  The simple solution here is to append .ToArray() like:

foreach (UserEntity u in Users.UserEntitySet.ToArray())

      // A Users.SaveChanges(true) here will work.

Life is good again, happy coding :-)

Adaugă comentariu