title: Auto-increment field on update in SQL Server and Entity Framework Core
body: <p>I'm working with a SQL Server database in which every table has a <code>Version</code> column which is an <code>INTEGER</code> value. Each time a row is updated, the version needs to be incremented by 1. Attempting to do an update with the same version number will throw an error.</p>
  
  <p>Because every table follows this convention, I wrote the Models used by EF Core to use a base class with the <code>Version</code> column and have all other Models deriving from that base class. Incrementation is then handled in the <code>DbContext SaveChanges()</code> method like this:</p>
  
  <pre><code>if (entry.State == EntityState.Added)
  {
      mb.Version = 1;
  }
  else if (entry.State == EntityState.Modified)
  {
      mb.Version += 1;
  }
  </code></pre>
  
  <p>This is starting to cause a problem though because if we update a Model in a View using ajax, for example, we have to track the version number and pass back the new value after updating, etc otherwise there's an error on the next update because we were still using the old version number.</p>
  
  <p>I'm looking for a way to just have the database increment this value on update and not have to worry about what it is in code at all. One option is to update our <code>Update</code> triggers on each table to increment the value, but there is well over 100 tables in the database. I was hoping there was some feature of EF Core I could apply to the <code>Version</code> column in the base class that would take care of this with a simple Migration.</p>