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

alattin (17125) 8 yıl önce sordu

Asp.Net MVC uygulamasında Entity Framework ile kaydetme işlemi yapmak istediğimde aşağıdaki hatayı alıyorum:

Server Error in '/' Application.

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: New transaction is not allowed because there are other threads running in the session.

Source Error: 
 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.


Stack Trace: 
 

[SqlException (0x80131904): New transaction is not allowed because there are other threads running in the session.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +717
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4515
   System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +84
   System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest) +1370
   System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest) +796
   System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName, Boolean shouldReconnect) +585
   System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel iso, String transactionName) +263
   System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel) +197
   System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel) +151

New transaction is not allowed because there are other threads running in the session. hatasının çözümü nedir?

Toplam 1 cevap


alattin (17125) 8 yıl önce cevapladı

New transaction is not allowed because there are other threads running in the session. hatasının çözümü nedir?

Bu hata ile aşağıdaki gibi bir kod blogunuz varsa karşılaşabilirsiniz

foreach (var _p in db.kategoriler)
                {
                    _p.HomePage = false;
                    db.Save(_p);
                }

Yukarıdaki örnekteki gibi bir foreach döngüsü içindeyken kayıt yapamazsınız. foreach döngüsü aktif bir okuyucu üzerinden çalıştırıldığından dolayı hata verecektir.

Sorun ToList() ile çözülebilir:

db.kategoriler.ToList()

Bu şekilde ToList() ile artık veriyi çekmiş ve aktif okuma işlemini sonlandırmış olacaksınız.