数据库操作预计影响 1 行,但实际影响 0 行 [英] Database operation expected to affect 1 row(s) but actually affected 0 row(s)

查看:58
本文介绍了数据库操作预计影响 1 行,但实际影响 0 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在两个表中插入记录,但出现异常.你能帮我解决这个问题吗.

I'm trying to insert records in two tables, but getting the exception. Could you please help me to resolve the issue.

首先我尝试了下面的代码.

First I tried the below code.

await _testRepository.InsertAsync(test);
await _xyzRepository.InsertAsync(xyz);

然后我尝试了这段代码,但没有任何效果.

Then I tried this code, But nothing is working for me.

try
{
   var test = new Test();

   using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
   {
      int? tenantId = _unitOfWorkManager.Current.GetTenantId();
      using (_unitOfWorkManager.Current.SetTenantId(tenantId))
      {
         await _testRepository.InsertAsync(test);

         var xyz = new XYZ();
         await _xyzRepository.InsertAsync(xyz);
         await _unitOfWorkManager.Current.SaveChangesAsync();
         await uow.CompleteAsync();
      }
   }
}
catch (Exception ex)
{
   throw new UserFriendlyException(ex.Message);
}

例外

消息:

数据库操作预计影响 1 行,但实际影响 0行.数据可能已被修改或删除,因为实体加载.请参阅 http://go.microsoft.com/fwlink/?LinkId=527962 了解有关理解和处理乐观并发的信息例外.

Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.

堆栈跟踪:

在Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected) atMicrosoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.d__4.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.d__2.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.d__32.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.d__10.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.d__7`2.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.d__61.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.d__59.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.EntityFrameworkCore.DbContext.d__48.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Abp.EntityFrameworkCore.AbpDbContext.d__49.MoveNext()在D:GithubaspnetboilerplatesrcAbp.EntityFrameworkCoreEntityFrameworkCoreAbpDbContext.cs:line214

at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.d__32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.d__7`2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.d__61.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.d__59.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.DbContext.d__48.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Abp.EntityFrameworkCore.AbpDbContext.d__49.MoveNext() in D:GithubaspnetboilerplatesrcAbp.EntityFrameworkCoreEntityFrameworkCoreAbpDbContext.cs:line 214

信息 2018-04-11 13:59:53,439 [3]ore.Mvc.Internal.ControllerActionInvoker - 执行动作方法MyCompany.MyProject.AdditionalMasterData.Tests.TestsAppService.CreateOrEdit(MyCompany.MyProject.Application) 带参数 ([CreateOrEditTestDto]) - ModelState 有效警告 2018-04-11 14:01:48,396 [4]Mvc.ExceptionHandling.AbpExceptionFilter - 预期的数据库操作影响 1 行,但实际上影响了 0 行.数据可能已经自加载实体以来已修改或删除.看http://go.microsoft.com/fwlink/?LinkId=527962 了解信息在理解和处理乐观并发异常.Abp.UI.UserFriendlyException: 数据库操作预计会影响 1行,但实际上影响了 0 行.数据可能已被修改或自加载实体后删除.看http://go.microsoft.com/fwlink/?LinkId=527962 了解信息在理解和处理乐观并发异常.在MyCompany.MyProject.AdditionalMasterData.Tests.TestsAppService.d__7.MoveNext()在C:RepoMyProjectVenuesaspnet-coresrcMyCompany.MyProject.ApplicationAdditionalMasterDataTestsTestsAppService.cs:line205--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在MyCompany.MyProject.AdditionalMasterData.Tests.TestsAppService.d__6.MoveNext()在C:RepoMyProjectVenuesaspnet-coresrcMyCompany.MyProject.ApplicationAdditionalMasterDataTestsTestsAppService.cs:line170--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__10.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext上下文)在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&接下来,范围&范围、对象&状态,布尔&已完成)在Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext()--- 从上一个抛出异常的位置开始的堆栈跟踪结束 --- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__23.MoveNext()

INFO 2018-04-11 13:59:53,439 [3 ] ore.Mvc.Internal.ControllerActionInvoker - Executing action method MyCompany.MyProject.AdditionalMasterData.Tests.TestsAppService.CreateOrEdit (MyCompany.MyProject.Application) with arguments ([CreateOrEditTestDto ]) - ModelState is Valid WARN 2018-04-11 14:01:48,396 [4 ] Mvc.ExceptionHandling.AbpExceptionFilter - Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions. Abp.UI.UserFriendlyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions. at MyCompany.MyProject.AdditionalMasterData.Tests.TestsAppService.d__7.MoveNext() in C:RepoMyProjectVenuesaspnet-coresrcMyCompany.MyProject.ApplicationAdditionalMasterDataTestsTestsAppService.cs:line 205 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MyCompany.MyProject.AdditionalMasterData.Tests.TestsAppService.d__6.MoveNext() in C:RepoMyProjectVenuesaspnet-coresrcMyCompany.MyProject.ApplicationAdditionalMasterDataTestsTestsAppService.cs:line 170 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__10.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__23.MoveNext()

更新

我找到了问题的根本原因.

I have got the root cause of the issue.

所以基本上我在 Entity2 上有一个插入触发器,当我在这个触发器内评论查询时,它工作正常.

So basically I have an insert trigger on Entity2 and When I have commented the query inside this trigger and then its working fine.

此触发器中大约有 10 个查询,很难知道是哪个查询导致了问题.那么你能告诉我如何调试这个触发器吗?

There are approximately 10 queries in this trigger and it's very hard to know which one is causing the problem. So could you please let me know how to debug this trigger?

推荐答案

确保在你的存储库函数 InsertAsync 中你没有调用 AddAsync,除非你正在使用 Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo 生成器.如文档中所述.AddAsync

Make sure in your repository function InsertAsync you are not calling AddAsync unless your are using the Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo generator. AS NOTED IN the Docs. AddAsync

这篇关于数据库操作预计影响 1 行,但实际影响 0 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆