title: DbUpdateConcurrencyException error when saving record in EF Core 1.1
body: <p>I have an ASP .Net Core 1.1 Web Api using EF Core 1.1</p>
  
  <p>I have a controller that gets triggered when a post is performed:</p>
  
  <pre><code>// POST: api/People
  [HttpPost]
  public async Task&lt;IActionResult&gt; PostPerson([FromBody] Person person)
  {
      if (!ModelState.IsValid)
      {
          return BadRequest(ModelState);
      }
  
      _context.Person.Add(person);
      await _context.SaveChangesAsync();
  
      string uri = CreateUri(person.PersonId);
      person.Uri = uri;
      await _context.SaveChangesAsync();
      return CreatedAtAction("GetPerson", new { id = person.Id }, person);
  }
  </code></pre>
  
  <p>As you can see, this controller adds a new Person record to the database. It then gets the newly created person's ID number to generate a URI to that person, and saves this URI back to the database. The first SaveChangesAsync() works perfectly and creates a new person record in the database. Immediately after that, the Person object is already automatically populated with the newly created Person ID. However, when I call the second SaveChangesAsync(), I get this error:</p>
  
  <blockquote>
    <p>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 href="http://go.microsoft.com/fwlink/?LinkId=527962" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=527962</a> for
    information on understanding and handling optimistic concurrency
    exceptions.</p>
    
    <p><strong>Source</strong> = "Microsoft.EntityFrameworkCore.Relational"</p>
    
    <p><strong>Target</strong> = ThrowAggregateUpdateConcurrencyException</p>
  </blockquote>
  
  <p><strong>Stack Trace:</strong></p>
  
  <blockquote>
    <p>at
    Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(Int32
    commandIndex, Int32 expectedRowsAffected, Int32 rowsAffected)    at
    Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.d__6.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.Internal.MySqlModificationCommandBatch.d__19.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.MySqlBatchExecutor.d__1.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__54.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__52.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__35.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 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
    at
    InspectionsWebApi.Controllers.PeopleController.d__5.MoveNext()
    in
    C:\Users\fabsr\Source\Repos\InspectionsWebApi\InspectionsWebApi\Controllers\PeopleController.cs:line
    78</p>
  </blockquote>
  
  <p>More specifically, this appears to be a DbUpdateConcurrencyException error.</p>
  
  <p>I am the only one accessing the database or using this Web API. 
  I've tried reattaching the object to the context just before the second SaveChanges:</p>
  
  <pre><code>...
  person.Uri = uri;
  _context.People.Attach(person);
  await _context.SaveChangesAsync();
  ...
  </code></pre>
  
  <p>But same error. I've tried re-fetching this person just after the first SaveChanges():</p>
  
  <pre><code>...
  person = _context.People.Find(person.Id);
  person.Uri = uri;
  await _context.SaveChangesAsync();
  ...
  </code></pre>
  
  <p>But same error.
  I've tried this just before the second SaveChanges():</p>
  
  <pre><code>...
  var entry = _context.Entry(person);
  entry.Property(e =&gt; e.Url).IsModified = true;
  await _context.SaveChangesAsync();
  ...
  </code></pre>
  
  <p>but same error...Any ideas? The backend is a MySQL Server database, and I am using the Pomelo framework to talk to the database.</p>