Hi,
I use OpenAccess in my asp mvc application. I decided for db first approach.
I need some help at handling db concurrency. I use MS SQL as DB server and all tables where concurrency has to be maintained have TimeStamp column.
I have produced a sample service named FundService with Update method:
01.public int Update(FundModel p_Fund)02.{03. using (EntitiesModel db = new EntitiesModel())04. {05. var fund = db.Funds.First(f => f.FundID == p_Fund.ID);06. 07. fund.Name = p_Fund.Name;08. fund.TimeStamp = p_Fund.TimeStamp;09. 10. try11. {12. db.SaveChanges();13. return p_Fund.ParentID;14. }15. catch (Telerik.OpenAccess.Exceptions.OptimisticVerificationException ex)16. {17. throw ex;18. }19. }20.}
The problem is that OptimisticVerificationException never occures.
I open two sessions and edit the same record(fund) in the Fund form (The record in both sessions are loaded first). On Save(Fund form is posted) the Update method is called.
I expect the second update fails because the view model has not been refreshed and second record is posted with old value of TimeStamp.
The problem is that before update the record has to be refreshed from DB (line 05). The existing TimeStamp value is overriden with the value from view model (line 08) but in where clause SQL update original value of TimeStamp is sent to DB.
I would ask you for proper pattern of db concurrency handling in code first approach.