How entity framework handles someone changing data underneath me (transactions)

Say have the following code:

public void SaveOrUpdate(OrderContract orderContract)
   foreach (var orderContract in orderContract.Tests)

      Order order=dataAccess.FindOne<Order>(x=>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 <= 0)


Does EF have anything that will care if someone updates an order after I retrieve it (in the FindOne call?

Or will it just happily overwrite any changes that have been done in between retrieving the data and my call to SaveChanges?

If it does not do that, then would a call ObjectContext.Connection.BeginTransaction be best to protect me? Or should I use new TransactionScope()?

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)).

Note: I am running with SQL Server 2008 R2 and EF 4.1


By default, yes it will overwrite changed records. If you are worried about it for your app, read this:

By default, the Entity Framework implements an optimistic concurrency model. This means that locks are not held on data in the data source between when the data is queried and the data is updated. The Entity Framework saves object changes to the database without checking for concurrency. For entities that might experience a high degree of concurrency, we recommend that the entity define a property in the conceptual layer with an attribute of ConcurrencyMode="fixed".....(follow link to read more)

Need Your Help

Search between 2 dates and update a GridView with ObjectDataSource

c# gridview objectdatasource

I am writing an ASP.NET application that has a GridView that uses an ObjectDataSource to fill it with data from a list of objects that is populated by a REST call. I also need to be able to search

Blom (or other rank-based) transformation in Java

java statistics

I'm trying to implement the Blom transformation in Java, that basically means back transforming ranks to a standard normal distribution.