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)
dataAccess.Add(orderedTest);
}
dataAccess.SaveChanges();
}
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:
http://msdn.microsoft.com/en-us/library/bb738618.aspx