title: Entity Framework handling concurrency
body: <p>Scenario:</p>
  
  <p>I have multiple accounts. Every account has a remaining credit. Every account corresponds to multiple users.</p>
  
  <p>I want the RemainingCredit field to correctly reflect the remaining credit (I'm using EF to abstract away the db).</p>
  
  <p><strong>E.G.</strong> In a scenario where initial credit is <strong>X</strong> and 2 users log in, one spends <strong>A</strong> , and one spends <strong>B</strong>, I want the end sum to be <strong>X-A-B</strong> (order of read/modify/save should be irrelevant).</p>
  
  <p>After reading up I think optimistic concurrency handling is what I'm looking for, as it scales well and this is very important to my scenario.
  The end procedure would be something like:</p>
  
  <pre><code>ComputeRemainingCredit(Double amountToBeDeducted) {
      Account acc = context.Accounts.Where(condition)
      ComputeRemainingCreditInternal(acc, amountToBeDeducted);
  }
  
  ComputeRemainingCreditInternal(Account acc, Double amountToBeDeducted) {
  try {
      acc.RemainingCredit = acc.RemainingCredit - amountToBeDeducted;
      context.SaveChanges();
  }
  catch (OptimisticConcurrencyException  ex) {
      context.Refresh(RefreshMode.StoreWinds, acc);
      //now I need to rerun the operation
      ComputeRemainingCreditInternal(acc, amountToBeDeducted);
  }
  }
  </code></pre>
  
  <ol>
  <li>Is this a correct interpretation/implementation of optimistic concurrency in EF?</li>
  <li>My end objective is to have the RemainingCredit field up to date. Are there any better solutions than OptimisticConcurrencyException?</li>
  </ol>