title: Database operation expected to affect 1 row(s) but actually affected 0 row(s) entity framework core
body: <p>I have the following code</p>
  
  <pre><code> try
      {
          using (var context = await PrepareDatabase())
          {
              // Update temp roles by removing one row, this should affect that RemoveOldRoles below is removing rows from the Role-table
              var entity = context.TempRawRoles.Where(x =&gt; x.Account == "brkar4").FirstOrDefault();
              context.TempRawRoles.Remove(entity);
              await context.SaveChangesAsync();
  
              await CleanUp(context);
          }
      }
      catch(Exception ex)
      {
          throw;
      }
  </code></pre>
  
  <p>Here is my CleanUp</p>
  
  <pre><code>   private async Task CleanUp(SecurityContext context)
          {
              try
              {
                  // Get only the global ids that belongs to our test accounts
                  List&lt;Guid&gt; ids = await GetCreatedTestGlobalIds(context);
  
                  List&lt;IdentityEntity&gt; identities = await context.Identities.Where(x =&gt; ids.Contains(x.GlobalId)).ToListAsync();
  
                  context.Identities.RemoveRange(identities);
  
                  await context.SaveChangesAsync(); // Throws error
  
              }
              catch(Exception ex)
              {
                  throw;
              }
          }
  </code></pre>
  
  <p>When <code>await context.SaveChangesAsync();</code> is executed in my CleanUp, I get the following 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>
  </blockquote>
  
  <p>I guess it has something todo with this: <code>context.TempRawRoles.Remove(entity);</code></p>
  
  <p>But how can I fix this?</p>