title: Best way to handle optimistic concurrency with Entity Framework 7
body: <p>I am using ASP.NET 5 rc1 and Entity 7 rc1 over coreclr with a sample code first-migration project.</p>
  
  <p>I was having a look at how to manage concurrency in entities but I couldn't find any good updated information about the recommended concurrency handling practices for Entity 7.</p>
  
  <p>I have a few entities, they all implement the interface:</p>
  
  <pre><code>public interface IVersionedModel
  {
      byte [] RowVersion { get; set; }
  }
  </code></pre>
  
  <p>as for example in:</p>
  
  <pre><code>public class User : IVersionedModel
  {
      public Guid UserId { get; set; }
      public string Name { get; set; }
      public byte[] RowVersion { get; set; }
  }
  </code></pre>
  
  <p>Instead adding the attribute <code>[Timestamp]</code> to each RowVersion column I prefer to use the fluent configuration, so in my DbContext I specify for each entity that I want that column to be handled as the row version.</p>
  
  <p><strong>However Entity 7 does not seem to support anymore the option <code>.IsRowVersion()</code></strong>
  but it has the option <code>IsConcurrencyToken()</code>, which should suffice but in code first migrations <strong>it generates a nullable varbinary column and when inserting/updating rows it does not auto-increment</strong>.</p>
  
  <p>I wonder whether the best thing to do is to explicitly manage the version of each row by increasing the version when updating or inserting entities in the database..
  This is my DbContext</p>
  
  <pre><code>public class AppDbContext : DbContext
  {
      public AppDbContext() : base()
      {
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Organization&gt; Organizations { get; set; }
      public DbSet&lt;Membership&gt; Memberships { get; set; }
  
      public override int SaveChanges()
      {
          //increase entity version for concurrency purposes
          foreach(var dbEntityEntry in ChangeTracker.Entries()
              .Where(x =&gt; x.State == EntityState.Added || x.State == EntityState.Modified))
          {
              IVersionedModel entity = dbEntityEntry.Entity as IVersionedModel;
              if(entity != null)
              {
                  //Increase byte[] RowVersion?
              }
          }
          return base.SaveChanges();
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;User&gt;().Property(u =&gt; u.RowVersion).IsConcurrencyToken();
          modelBuilder.Entity&lt;Organization&gt;().Property(o =&gt; o.RowVersion).IsConcurrencyToken();
          modelBuilder.Entity&lt;Membership&gt;().Property(m =&gt; m.RowVersion).IsConcurrencyToken();
      }
  }
  </code></pre>
  
  <p>Basically my question is, how to handle concurrency with ASP.NET 5 and Entity 7?
  It's the first time I deal with this thing and any suggestion will be appreciated.</p>
  
  <p><strong>UPDATE</strong>: Thanks to the link <a href="https://stackoverflow.com/users/1922568/joe-audette">https://stackoverflow.com/users/1922568/joe-audette</a> provided I found some answers, unfortunately not all</p>
  
  <p>As per <a href="http://docs.efproject.net/en/latest/modeling/concurrency.html#how-concurrency-tokens-work-in-ef" rel="nofollow noreferrer">http://docs.efproject.net/en/latest/modeling/concurrency.html#how-concurrency-tokens-work-in-ef</a>
  it seems we can use <code>Timestamp</code> annotation if we want to version a row or <code>ConcurrencyCheck</code> annotation on a property if we want to version based on a property (not the whole row).</p>
  
  <p>The fluent API only allows to configure concurrency token by using <code>.IsConcurrencyToken()</code></p>
  
  <p>I haven't found yet a way through the fluent API to version the whole row with a dedicated column (it'd be nice if the fluent API allowed an <code>.IsRowVersion()</code>, but it doesn't)</p>