1

現在、このチュートリアルに従っています

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

ただし、同時実行例外 (DbUpdateConcurrencyException) が発生するようには見えません。

関連するテーブルにタイムスタンプ フィールドを作成しました (私のプロジェクトでは「グループ」と呼ばれます)。

私のコードは次のとおりです

GroupController.cs で

[HttpPost]
public ActionResult Edit(Group group)
{
    try
    {
        if (ModelState.IsValid)
        {

            medEntitiesDB.Entry(group).State = EntityState.Modified;
            medEntitiesDB.SaveChanges();
            return RedirectToAction("Index");

        }


    }

    catch (DbUpdateConcurrencyException ex)
    {
        var entry = ex.Entries.Single();
        var databaseValues = (Group)entry.GetDatabaseValues().ToObject();
        var clientValues = (Group)entry.Entity;
        if (databaseValues.GroupName != clientValues.GroupName)
            ModelState.AddModelError("Name", "Current value: "
                + databaseValues.GroupName);
        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.");
        group.Timestamp = databaseValues.Timestamp;
    }
    catch (DataException)
    {
        //Log the error (add a variable name after Exception)
        ModelState.AddModelError(string.Empty, "Unable to save changes. 
          Try again, and if the problem persists contact your system administrator.");
    }

    return View(group);
}

私の「グループ」クラスは次のとおりです

namespace MyClasses
{
using System;
using System.Collections.Generic;

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;


 public partial class Group
 {

    public System.Guid GroupID { get; set; }
    public string GroupName { get; set; }
    public System.DateTime DateCreated { get; set; }

    [Timestamp]
    public byte[] Timestamp { get; set; }
 }
}

この例外が発生しない理由を知っている人はいますか?

4

1 に答える 1

0

モデル バインディングを通じて Timestamp 値を指定したことを確認してください。

また、別の人がグループ オブジェクトを変更する必要もあります。編集ページを 2 回開き、いくつかのフィールドを変更して、[保存] ボタンをクリックします。例外が発生するはずです。

于 2013-04-23T15:24:22.967 に答える