title: EF Core 2.1.1 "DbUpdateConcurrencyException" when calling SaveChangesAsync() using IBM DB2
body: <p>I'm trying to switch my database provider from SqlServer to Db2.
  Now I'm facing the issue that when I call:</p>
  
  <pre><code>await SaveChangesAsync()
  </code></pre>
  
  <p>EF drops the error</p>
  
  <blockquote>
    <p>DbUpdateConcurrencyException: 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.</p>
  </blockquote>
  
  <p>the synchronous operation works fine.</p>
  
  <pre><code>        await context.SaveChangesAsync(); // doesn't work
  
          context.SaveChanges(); // works
  </code></pre>
  
  <p>The simplest thing would be to make it synchronous, but I'm also using asp.net  core identity which completely freaks out.</p>
  
  <p>I have no more ideas how to solve this issue, maybe it is bug. Thought it could be like <a href="https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/398" rel="nofollow noreferrer">this</a> because i used EF Core 2.0 before, but after successfully upgrading to 2.1.1. The error still drops.</p>
  
  <p>Could it be a provider issue? Using the newest version of the IBM.Db2 Provider.</p>
  
  <p>The common database at my company is db2, that's why I have to change the provider.</p>
  
  <p><strong>Update:</strong><br>
  The problem seems to happen only when I'm trying to do an INSERT.</p>
  
  <p>in Startup.cs</p>
  
  <pre><code>services.AddDbContext&lt;MyDbContext&gt;(options =&gt; {
  options.UseLoggerFactory(Config.DbLogger);
  
  options.UseDb2(Configuration.GetConnectionString("DB2Connection"), config =&gt;
  {
      config.SetServerInfo(IBM.EntityFrameworkCore.Storage.Internal.IBMDBServerType.LUW, IBM.EntityFrameworkCore.Storage.Internal.IBMDBServerVersion.LUW_11_01_2020);
  });
  });
  </code></pre>
  
  <p>MyContext.cs</p>
  
  <pre><code>public class MyContext : DbContext
  {
      public DbSet&lt;Event&gt; Events { get; set; }
      protected override void OnModelCreating(ModelBuilder builder)
      {
          builder.Entity&lt;Event&gt;().ToTable("Events");
      }
  }
  </code></pre>
  
  <p>MyController.cs</p>
  
  <pre><code>    public class MyController : Controller
      {
          private readonly MyContext context;
          public MyController(MyContext _context)
          {
              context = _context;
          }
  
          [HttpPost]
          public async Task&lt;IActionResult&gt; Create()
          {
              var _event = new Event { ID = Guid.NewGuid().ToString(), UserID = "adwadawdawdaw", TargetID = "1", Target = "todo", TimeStamp = DateTime.Now };
              await context.Events.AddAsync(_event);
              await context.SaveChangesAsync();   // &lt;- Fails with "DbUpdateConcurrencyException"
              // context.SaveChanges(); // &lt;- works!
  
              return Ok();
          }
          [HttpPut]
          public async Task&lt;IActionResult&gt; Update()
          {
              var _event = context.Events.FirstOrDefault();
              _event.TargetID = "2";
              await context.SaveChangesAsync(); // &lt;- works!
              return Ok();
          }
          [HttpDelete]
          public async Task&lt;IActionResult&gt; Delete()
          {
              var _event = context.Events.FirstOrDefault();
              context.Events.Remove(_event);
              await context.SaveChangesAsync();   // &lt;- works
              return Ok();
          }
      }
  </code></pre>
  
  <p>Connectionstring</p>
  
  <blockquote>
    <p>Server=localhost:50000;Database=db1;UID=user;PWD=password;CurrentSchema=todo;</p>
  </blockquote>
  
  <p>DB2 Version 11.1</p>
  
  <p>Table:
  <a href="https://i.stack.imgur.com/5T5Iv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5T5Iv.png" alt="enter image description here"></a></p>
  
  <p>Error message:</p>
  
  <pre><code>DbUpdateConcurrencyException: 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(int commandIndex, int expectedRowsAffected, int rowsAffected)
  Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeResultSetWithoutPropagationAsync(int commandIndex, RelationalDataReader reader, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(DbContext _, ValueTuple&lt;IEnumerable&lt;ModificationCommandBatch&gt;, IRelationalConnection&gt; parameters, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IReadOnlyList&lt;InternalEntityEntry&gt; entriesToSave, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken)
  Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken)
  </code></pre>