Home > database >  The database operation was expected to affect 1 row(s) but actually affected 92085 row(s) data may h
The database operation was expected to affect 1 row(s) but actually affected 92085 row(s) data may h

Time:04-20

I am having this piece of code on my GraphQL .NET server.
It is supposed to do a simple update query.
Note that delete, create and read are OK.
All the updates possibilites commented above are all giving me this error:

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 92085 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 Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32 commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagation(Int32 commandIndex, RelationalDataReader reader)
   at Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.Consume(RelationalDataReader reader)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__104_0(DbContext _, ValueTuple`2 t)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
   at GlanumAppsApi.Controllers.TimeTrackingController.CreateTimeTracking(TimeTrackingModel timeTracking) in C:\Users\Edouard\RiderProjects\GlanumAppsApi\GlanumAppsApi\Controllers\TimeTrackingController.cs:line 62

I tried a lot of thing.
I tried concurrency token, timestamp, and absolutely all that can exists on Google.

    public TimeTrackingModel CreateTimeTracking(TimeTrackingModel timeTracking)
    {
        try
        {
            var ctx = _context();
            if (timeTracking.Id > 0)
            {
                
                /*
                var entry = ctx.TimeTrackings.Find(timeTracking.Id)!;
                ctx.Entry(entry).CurrentValues.SetValues(timeTracking);
                //ctx.TimeTrackings.Update(entry);
                var test = ctx.ChangeTracker.ToDebugString();
                ctx.SaveChanges();
                */
                

                
                var entry = ctx.TimeTrackings.Find(timeTracking.Id)!;
                entry.Commentaire = "ziofjzie";
                // ctx.Entry(entry).CurrentValues.SetValues(timeTracking);
                // ctx.TimeTrackings.Update(entry);
                // ctx.Entry(entry).State = EntityState.Modified;
                var test = ctx.ChangeTracker.ToDebugString();
                ctx.SaveChanges();
                

                /*
                ctx.TimeTrackings.Attach(timeTracking);
                ctx.TimeTrackings.Update(timeTracking);
                var test = ctx.ChangeTracker.ToDebugString();
                ctx.SaveChanges();
                */
                
            }
            else
            {
                ctx.TimeTrackings.Add(timeTracking);
                ctx.SaveChanges();
            }

            return timeTracking;
        }
        catch
        {
            return new TimeTrackingModel();
        }
    }

The test variable, in one of my test with that error, gave me this result:

TimeTrackingModel {Id: 92085} Modified
  Id: 92085 PK
  Chevauchement: <null> Modified
  Commentaire: 'ziofjzie' Modified Originally 'test'
  DateCreation: <null> Modified
  DateModification: <null> Modified
  DateSuppression: <null> Modified
  Debut: '22/04/1978 06:00:00' Modified
  EstDeductible: 'True' Modified
  Fin: '22/04/1981 06:00:00' Modified
  Id_Tache: 1 FK Modified
  Id_Utilisateur: 5 FK Modified
  Tache: <null>
  Utilisateur: <null>

As you can see, entity framework core 6 is giving me the right changes that was expected, but still throwing that error.
Here is the database entries:

Id  Debut   Fin DateCreation    DateModification    DateSuppression Id_Utilisateur  Id_Tache    Chevauchement   Commentaire estDeductible
92086   1979-04-22 06:00:00.000 1982-04-22 06:00:00.000 NULL    NULL    NULL    5   1   _92085_ sdfsdfsdf   1
92085   1978-04-22 06:00:00.000 1981-04-22 06:00:00.000 NULL    NULL    NULL    5   1   _92086_ test    1

CodePudding user response:

Solved by @David Browne - Microsoft I turned off triggers by

DISABLE TRIGGER TimeTracking_OnInsertUpdateDelete on TimeTracking;
DISABLE TRIGGER TimeTracking_AspNet_SqlCacheNotification_Trigger on TimeTracking;

It is now working ! :)

  • Related