使用entry.Reload()&处理并发异常。 ModelState.Clear()

时间:2013-07-14 17:14:00

标签: c# entity-framework

我有以下行动方法: -

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Group group)
{
    try
    {
        if (ModelState.IsValid)
        {
            AuditInfo auditinfo = repository.IntiateAudit(2, 2, User.Identity.Name, 2);
            groupRepository.InsertOrUpdate(group);
            groupRepository.Save();
            repository.InsertOrUpdateAudit(auditinfo);
            return RedirectToAction("Index");
        }
    }
    catch (DbUpdateConcurrencyException ex)
    {
        var entry = ex.Entries.Single();
        entry.Reload();
        ModelState.Clear();
        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);
}

目前上面的代码工作正常,但我不确定是否写.Reload()&异常中的.Clear()方法是一种很好的做法,还是认为编程不好?

谢谢!

0 个答案:

没有答案