I'm seeing some strange race conditions in my webapp that I suspect may be due to the Entity Framework handling read locks in an unexpected way. When a request is made in my application to any page, I automatically load up the account model which is then stored in my DbContext for the lifetime of the request. Some web pages need to lock the account DB row so I can safely do some other operations without race conditions. Here's how I'm doing this now...
//... code that begins the request and loads the account into context.
// Some pages may run code that looks something like this.
using(var tran = existingCtx.Database.BeginTransaction(IsolationLevel.RepeatableRead))
{
// Lock customer.
var act = ctx.Accounts.Find(purchaseFor.ID);
if (act == null)
throw new RecordNotFoundException("Unable to find specified customer.");
DoStuffRelyingOnLock();
Commit();
}
Will the call find Find(purchaseFor.ID) LOCK the account row in the database even if it's already loaded into context?