title: How entity framework handles someone changing data underneath me (transactions)
body: <p>Say have the following code:</p>
  
  <pre><code>public void SaveOrUpdate(OrderContract orderContract)
  {
     foreach (var orderContract in orderContract.Tests)
     {
  
        Order order=dataAccess.FindOne&lt;Order&gt;(x=&gt;x.OrderId==orderContract.OrderId) 
                    ?? new Order();
  
        // Where there are updates, put the stuff in the 
        // contract over what we already had.
        Mapper.Map(orderContract, order);
  
         // if it is new then add it in so it is inserted by EF.
        if (orderedTest.OrderedTestId &lt;= 0)
            dataAccess.Add(orderedTest);
  
     }
     dataAccess.SaveChanges();
  }
  </code></pre>
  
  <p>Does EF have anything that will care if someone updates an order after I retrieve it (in the <code>FindOne</code> call?</p>
  
  <p>Or will it just happily overwrite any changes that have been done in between retrieving the data and my call to <code>SaveChanges</code>?</p>
  
  <p>If it does not do that, then would a call <code>ObjectContext.Connection.BeginTransaction</code> be best to protect me?  Or should I use <code>new TransactionScope()</code>?</p>
  
  <p>And how can either of those know what rows I need to have in a transaction.  (Just because I read a row does not mean that I want it locked.  Or does it lock all tables in the model (yuck)).</p>
  
  <p>Note: I am running with SQL Server 2008 R2 and EF 4.1</p>