title: Entity Framework Concurrency insertion issue
body: <p>We are working on a MVC application. In this application we have a payment module. Once user starting a recurring subscription, application will get two responses from paypal for payment complete, with the same TransactionId.
  One is through “Success Url” and other one is through IPN listener.
  We are using a “Transaction”table to keep paypal transaction details.
   Application will check whether the “TransactionId” exist in the database, while getting a response from Paypal.  So  net result is first response from paypal will insert to “Transaction” table.</p>
  
  <p>Recently  We are  having issues related with Entity Frame work  concurrency. If the two response parellay comes,  both the two records are inserting to the transaction table  with the same “trnasction id”, even if we have the code for check existence of transactionid  .
  How do we prevent this duplicate insertion? 
  Both insertion is happening from different CONTEXT.</p>
  
  <pre><code>       var ipnDetail = unitOfWork.TransactionDetailRepository.GetTransaction(transactionNumber);
              if (ipnDetail == null)
              {
  }
  </code></pre>
  
  <p>We are using same code for both insertion. Only difference is we are calling from different EF Context.</p>
  
  <p><a href="https://i.stack.imgur.com/53ZdO.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/53ZdO.jpg" alt="enter image description here"></a></p>
  
  <p>You can also note the first inserted entry having greater time than second inserted record. Actually we are setting the date from code. 
  How do we solve this concurrency issue?</p>
  
  <p>We tried to  use a “ObjectContext.Refresh” for a solution. But it does not help us.</p>
  
  <pre><code>((IObjectContextAdapter)context).ObjectContext.Refresh(System.Data.Objects.RefreshMode.StoreWins, ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added));
  </code></pre>
  
  <p>Any help would be appreciable.  Please note that application is in production environment. </p>
  
  <p>Best Regards,</p>
  
  <p>Ranish</p>