奇怪的实体框架异常

时间:2016-11-18 09:51:24

标签: c# .net multithreading entity-framework entity-framework-6

我有非常简单的方法,有时(可能在高负荷下)它抛出非常奇怪的例外。其中一个

  • ROLLBACK TRANSACTION请求没有相应的BEGIN TRANSACTION

  • 不允许启动新请求,因为它应附带有效的事务描述符

  • 该连接不支持MultipleActiveResultSets

Bellow你可以看到方法和异常的堆栈

        using (var context1 = cp.GetNewContext())
        {
           //some work here
             await context1.SaveChangesAsync();
        }

        using (var context2 = cp.GetNewContext())
        {
            var entity = context2.Entities 
                .FirstOrDefault(e => e.ClassId == classId)
                          ?? context2.Entities.Add(new Entitity());

            entity.ClassId = classId;
            entity.Date = DateTime.UtcNow;

            context2.SaveChanges();
        }
     }

有人可以帮助我吗?我应该搜索什么?

例外

  • ROLLBACK TRANSACTION请求没有相应的BEGIN TRANSACTION。

      

    System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常---> System.Data.SqlClient.SqlException:ROLLBACK TRANSACTION请求没有相应的BEGIN TRANSACTION。在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() at System.Data.SqlClient.SqlDataReader.get_MetaData() at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource 1完成,Int32超时,任务和任务,布尔asyncWrite)处于System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, System.Data.SentClination.Interrastructure.Interception中System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)的System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior,String方法)中的RunBehavior runBehavior,Boolean returnStream,String方法) .InternalDispatcher 1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func 3操作,TInterceptionContext interceptionContext,Action 3 executing, Action 3执行)在System.Data.Entity的System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand命令,DbCommandInterceptionContext interceptionContext)。 Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior)---内部异常堆栈跟踪结束---在System.Data.Entity。 System.Data.Entity.Core上的System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute [TResultType](ObjectContext context,ObjectParameterCollection parameterValues)中的Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior behavior) .Objects.ObjectContext.ExecuteInTransaction [T](Func 1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery 1.<> c__DisplayClass7.b__5()在System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute [TResult](Func 1 operation) at System.Data.Entity.Core.Objects.ObjectQuery 1 .GetResults(Nullable 1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery 1..GetEnumerator> b__0()at System.Data.Entity.Internal.LazyEnumerator 1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable 1 source)at System.Linq.Queryable.FirstOrDefault [TSource](IQueryable {{ 1}} 1谓词)

  • 不允许启动新请求,因为它应该带有有效的事务描述符

      

    System.Data.Entity.Infrastructure.DbUpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常---> System.Data.Entity.Core.UpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常---> System.Data.SqlClient.SqlException:不允许启动新请求,因为它应该带有有效的事务描述符。在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action 1 source, Expression 1完成,Int32超时,任务和任务,布尔asyncWrite)处于System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource {{ 1}} 1.Dispatch [TTarget,TInterceptionContext,TResult](TTarget target,Func 1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource 3执行,Action 1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Entity.Infrastructure.Interception.InternalDispatcher 2 identifierValues,List 3 operation, TInterceptionContext interceptionContext, Action 1 func,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction,系统上的System.Data.Entity.Infrastructure.DbExecutionStrategy.Execute [TResult](Func`1操作)的System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction)中的布尔releaseConnectionOnSuccess) System.Data.Entity.Internal.InternalContext.SaveChanges()

    中的.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions选项,Boolean executeInExistingTransaction)
  • 该连接不支持MultipleActiveResultSets

      

    System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常---> System.InvalidOperationException:连接不支持MultipleActiveResultSets。在System.Data.SqlClient.SqlCommand.b__24(任务3 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary 2.InnerInvoke()在System.Threading.Tasks.Task.Execute()---从先前位置抛出异常的堆栈跟踪结束 - - System.Data.EntityClient.Internal.EntityCommandDefinition.d__c.MoveNext()中System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) - - 内部异常堆栈跟踪的结束---在System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.d__c.MoveNext()---从抛出异常的上一个位置的堆栈跟踪结束---在系统位于System.Runtime.CompilerServices.TaskAwaiter 1 generatedValues) at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() --- End of inner exception stack trace --- at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)1. System.Data.Entity的1.GetResult() .Core.Objects.Internal.ObjectQueryExecutionPlan.d__0 1 result) at System.Threading.Tasks.ContinuationResultTaskFromResultTask 1.GetResult()在System。 Data.Entity.Utilities.TaskExtensions.CultureAwaiter 1.GetResult() at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter 1.MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在System.Runtime.CompilerServices.TaskAwaiter 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)处。在System.Data.Entity.Infrastructure.DbExecutionStrategy.d__9 1.GetResult() at System.Data.Entity.Core.Objects.ObjectContext.d__3d 1的GetResult() .d__e.MoveNext()---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处)System.Data.Entity.Internal.LazyAsyncEnumerator 1.GetResult() at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter 1.GetResult()at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter 1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Data.Entity.Core.Objects.ObjectQuery 1.MoveNext()---堆栈跟踪结束从抛出异常的先前位置---在System.Runtime.CompilerServices.TaskAwaiter.ThrowF System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.d__1a 1.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter中的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中的orNonSuccess(任务任务)。

0 个答案:

没有答案