Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Database operation expected to affect 1 row(s) but actually #4976


User avatar
0
manojreddy created

I'm trying to insert records in two tables, but getting the exception.

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);
}

Exception

Message : 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 <a class="postlink" href="http://go.microsoft.com/fwlink/?LinkId=527962">http://go.microsoft.com/fwlink/?LinkId=527962</a> for information on understanding and handling optimistic concurrency exceptions.

stack trace : at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected) at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.<ConsumeResultSetWithPropagationAsync>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.<ConsumeAsync>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.<ExecuteAsync>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.<ExecuteAsync>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.<ExecuteAsync>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.

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.


6 Answer(s)
  • User Avatar
    0
    manojreddy created

    Any update please?

  • User Avatar
    0
    alper created
    Support Team

    It's related to your entities. To trace the problem create 2 new entities Entity1 and Entity2 with just 1 integer property. And write the same code for Entity1 and Entity2.

    public class Entity1: Entity
    {
    	public virtual string Variable1 { get; set; }
    }
    
    public class Entity2: Entity
    {
    	public virtual string Variable2 { get; set; }
    }
    
    try
    {
       using (var uow = _unitOfWorkManager.Begin(TransactionScopeOption.RequiresNew))
       {
    //     No need the below lines! You are setting the same tenantId.
    //      int? tenantId = _unitOfWorkManager.Current.GetTenantId();
     //     using (_unitOfWorkManager.Current.SetTenantId(tenantId))
    //      {
             await _testRepository.InsertAsync(test);
    
             var entity1 = new Entity1 ();
             var entity2 = new Entity2 ();
    
             await _entity1Repository.InsertAsync(entity1);
             await _entity2Repository.InsertAsync(entity2);
    
             await _unitOfWorkManager.Current.SaveChangesAsync();
             await uow.CompleteAsync();
    //      }
       }
    }
    catch (Exception ex)
    {
       throw new UserFriendlyException(ex.Message);
    }
    

    If it's related to your entities, open your SQL Profiler and see what's going on. <a class="postlink" href="https://docs.microsoft.com/en-us/ef/core/saving/concurrency">https://docs.microsoft.com/en-us/ef/cor ... oncurrency</a>

  • User Avatar
    0
    manojreddy created

    I have got the root cause of the issue.

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

    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?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Are you talking about SQL Server triggers ?

  • User Avatar
    0
    manojreddy created

    Yes, SQL Server Triggers.

  • User Avatar
    0
    alper created
    Support Team

    you can just understand disabling them one by one.