title: Is it possible for data to change within a transaction with repeatable read isolation?
body: <p>I have some .NET code wrapped up in a repeatable read transaction that looks like this:</p>
  
  <pre><code>using (
                  var transaction = new TransactionScope(
                      TransactionScopeOption.Required,
                      new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead },
                      TransactionScopeAsyncFlowOption.Enabled))
              {
                  int theNextValue = GetNextValueFromTheDatabase();
                  var entity = new MyEntity
                                 {
                                     Id = Guid.NewGuid(),
                                     PropertyOne = theNextValue, //An identity column
                                     PropertyTwo = Convert.ToString(theNextValue),
                                     PropertyThree = theNextValue,
                                     ...
                                 };
                  DbSet&lt;MyEntity&gt; myDbSet = GetEntitySet();
                  myDbSet.Add(entity);
                  await this.databaseContext.Entities.SaveChangesAsync();
  
                  transaction.Complete();
              }
  </code></pre>
  
  <p>The first method, <code>GetNextValueFromTheDatabase</code>, retrieves the max value stored in a column in a table in the database.  I'm using repeatable read because I don't want two users to read and use the same value.  Then, I simply create an <code>Entity</code> in memory and call <code>SaveChangesAsync()</code> to write the values to the database.</p>
  
  <p>Sporadically, I see that the values of entity.PropertyOne, entity.PropertyTwo, and entity.PropertyThree do not match each other.  For example, entity.PropertyOne has a value of 500, but entity.PropertyTwo and entity.PropertyThree have a value of 499.  How is that possible?  Even if the code weren't wrapped in a transaction, I would expect the values to match (just maybe duplicated across the Entities if two users ran at the same time).</p>
  
  <p>I am using Entity Framework 6 and Sql Server 2008R2.</p>
  
  <p><strong>Edit:</strong><br>
  Here is the code for <code>GetNextValueFromTheDatabase</code></p>
  
  <pre><code>public async Task&lt;int&gt; GetNextValueFromTheDatabase()
  {
      return await myQuerable
          .OrderByDescending(x =&gt; x.PropertyOne) //PropertyOne is an identity column (surprise!)
          .Select(x =&gt; x.PropertyOne)
          .Take(1)
          .SingleAsync() + 1;
  }
  </code></pre>