Guest User

DbUpdateConcurrencyException will not be raised for entity framewrok

a guest
Apr 5th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. public ActionResult Edit(int id, FormCollection collection)
  2. {
  3. Class c = repository.GetClass(id);
  4. try
  5. {
  6. if (TryUpdateModel(c))
  7. {
  8. epository.Save();
  9. return RedirectToAction("Details", new { id = c.ClassID });
  10. }
  11. }
  12.  
  13. catch (DbUpdateConcurrencyException ex)
  14. {
  15. var entry = ex.Entries.Single();
  16. var databaseValues = (Class)entry.GetDatabaseValues().ToObject();
  17. var clientValues = (Class)entry.Entity;
  18. if (databaseValues.ClassName != clientValues.ClassName)
  19. ModelState.AddModelError("Name", "Current value: "
  20. + databaseValues.ClassName);
  21.  
  22. ModelState.AddModelError(string.Empty, "The record you attempted to edit "
  23. + "was modified by another user after you got the original value. The "
  24. + "edit operation was canceled and the current values in the database "
  25. + "have been displayed. If you still want to edit this record, click "
  26. + "the Save button again. Otherwise click the Back to List hyperlink.");
  27. c.Timestamp = databaseValues.Timestamp;
  28. }
  29. catch (DataException)
  30. {
  31. //Log the error (add a variable name after Exception)
  32. ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");
  33. }
  34.  
  35. return View(c);
  36. }
Add Comment
Please, Sign In to add comment