0

我有以下 Action 方法,fr 编辑一个对象,我正在检查并发异常:-

[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。那么可能是什么原因造成的呢?

4

0 回答 0