title: C# - Entity Framework Core - Database operation expected to affect 1 row(s) but actually affected 0 row(s)
body: <p>I'm building an n-tier web app in ASP.NET Core 3.1 that uses Entity Framework to interact with a SQL Server database. Everything works fine except for updating an entity twice without refreshing in between both requests.</p>
  <p>So the first time I read and entity from the database and update it, it works perfectly. When I try to update the entity immediately after, I get the following exception:</p>
  <blockquote>
  <p>Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s).<br />
  Data may have been modified or deleted since entities were loaded...</p>
  </blockquote>
  <p>I'm using the generic repository pattern in combination with asynchronous code.</p>
  <p>An overview of my code:</p>
  <p>GenericRepository.cs:</p>
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class
  {        
      protected readonly DataContext _context;
      protected DbSet&lt;T&gt; _entities;
      
      public Repository(DataContext context)
      {
          this._context = context;
          _entities = context.Set&lt;T&gt;();
      }
  }
  </code></pre>
  <p>MemberRepository.cs:</p>
  <pre><code>public class MemberRepository : Repository&lt;Member&gt;, IMemberRepository
  {    
      public MemberRepository(DataContext context) : base(context)
      {
          //
      }
  
      public async override Task&lt;Member&gt; SelectByIdAsync(int id)
      {
          return await this._entities
                      .Where(m =&gt; m.Id == id)
                      .Include(m =&gt; m.Addresses)
                      .Include(m =&gt; m.MemberHobbies)
                      .Include(m =&gt; m.Car)
                      .Include(m =&gt; m.EmailAddresses)
                      .FirstOrDefaultAsync();
          
          //return await this._context.Members.Where(m =&gt; m.Id == id).Include(m =&gt; m.Addresses).Include(m =&gt; m.MemberHobbies).Include(m =&gt; m.Car).Include(m =&gt; m.EmailAddresses).FirstOrDefaultAsync();
      }
  
      public async override Task UpdateAsync(Member member)
      {
          this._context.Members.Update(member);
          //this._context.Set&lt;Member&gt;().Update(member);
          await this._context.SaveChangesAsync();
      }
  }
  </code></pre>
  <p>startup.cs:</p>
  <pre><code>services.AddScoped(typeof(IRepository&lt;&gt;), typeof(Repository&lt;&gt;));
  services.AddScoped&lt;IMemberRepository, MemberRepository&gt;();
  </code></pre>
  <p>I have tried to change the way I read or update my entity (see the commented out lines), but those give the same result. Also: I have other entities which work perfectly, the only difference is that the Member class has multiple foreign keys (see .Include(...))</p>
  <p>Might it have anything to do with improper use of asynchronous functions? Maybe because I'm not returning the updated Member and thus next time I execute an update I'm sending a now out of date member?</p>
  <p>Also: I found <a href="https://stackoverflow.com/questions/53676084/entity-framework-core-database-operation-expected-to-affect-1-rows-but-actual">this answer</a>, but it makes no sense to me at all</p>
  <p>Thanks!</p>