オブジェクトを編集するための次の Action メソッドがあり、同時実行例外をチェックしています:-
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Group group)
{
try
{
if (ModelState.IsValid)
{
groupRepository.InsertOrUpdate(group);
groupRepository.Save();
return RedirectToAction("Index");
}
}
catch (DbUpdateConcurrencyException ex)
{
var entry = ex.Entries.Single();
var clientValues = (Group)entry.Entity;
ModelState.AddModelError(string.Empty, "The record you attempted to edit "
+ "was modified by another user after you got the original value. The "
+ "edit operation was canceled and the current values in the database "
+ "have been displayed. If you still want to edit this record, click "
+ "the Save button again. Otherwise click the Back to List hyperlink.");
}
catch (DbUpdateException)
{
ModelState.AddModelError(string.Empty, " An Error Occured, please check the Group name for uniquenee.");
}
catch (DataException)
{
ModelState.AddModelError(string.Empty, "Unable to save changes. Try again, and if the problem persists contact your system administrator.");}
return View(group);
}
そして、私は次のリポジトリメソッドを持っています:-
public void InsertOrUpdate(Group group)
{
if (group.GroupID == default(int)) {
// New entity
context.Groups.Add(group);
} else {
// Existing entity
context.Entry(group).State = EntityState.Modified;
}
}
最後に、ビューに次を追加しました:-
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.GroupID)
@Html.HiddenFor(model => model.timestamp)
@Html.Partial("_CreateOrEdit", Model)
ただし、現在、ユーザーがオブジェクトを編集しようとすると、DbUpdateConcurrency が発生します。何がこれを引き起こしているのでしょうか?