3

我目前正在关注本教程的并发处理。

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。我在 EF 5.0 中使用数据库优先方法我有 Carrier 对象,与 Address & Contact 对象的关系为 0..1。

我在 Carrier 表中创建了一个 RowVersion 字段。

我的代码如下:

public Carrier UpdateCarrier(Carrier carrier)
    {
        try
        {
            var entry = ariesConfigContext.Entry<Carrier>(carrier);
            if(entry.State == System.Data.EntityState.Detached)
            {
                var attachedEntity = ariesConfigContext.Carriers.Include(con => con.Contact).Include(a => a.Address).Single(c => c.CarrierID == carrier.CarrierID);
                if(attachedEntity != null)
                {
                    var attachedEntry = ariesConfigContext.Entry(attachedEntity);
                    attachedEntry.CurrentValues.SetValues(carrier);
                    attachedEntity.Contact = carrier.Contact;
                    attachedEntity.Address = carrier.Address;
                }
                else
                {
                    entry.State = System.Data.EntityState.Modified;
                }
            }              
            ariesConfigContext.SaveChanges();
        }
        catch(DbUpdateConcurrencyException ex)
        {
            //do something
        }
        return carrier;
    }

我的模型如下:

namespace Aries.Domain.Models.AriesConfigDB
{
using System;
using System.Collections.Generic;

public partial class Carrier
{
    public Carrier()
    {
        this.UserProfiles = new HashSet<UserProfile>();
    }

    public int CarrierID { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public string Memo { get; set; }
    public string DBServerName { get; set; }
    public string DBName { get; set; }
    public string DBUser { get; set; }
    public string DBPassword { get; set; }
    public Nullable<int> AddressID { get; set; }
    public Nullable<int> ContactID { get; set; }
    public bool IsDeleted { get; set; }
    public byte[] RowVersion { get; set; }

    public virtual Address Address { get; set; }
    public virtual Contact Contact { get; set; }
    public virtual ICollection<UserProfile> UserProfiles { get; set; }
}

}

我的观点是:

@model Aries.Domain.Models.AriesConfigDB.Carrier

<div>
<div class="message-error">
    @if (TempData["ErrorMessage"] != null)
    {
        <p>@TempData["ErrorMessage"]</p>
    }
</div>

@Html.HiddenFor(model => model.CarrierID)
    @Html.HiddenFor(model => model.RowVersion)

<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Name)
    @Html.ValidationMessageFor(model => model.Name)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Abbreviation)
</div>
....
    ....

我无法引发异常。任何帮助表示赞赏。

更新:我已经有一个如下所示的 Carrier 元数据类,并且在那里添加了 [Timestamp] 属性。

public class CarrierMetadata
{
    public int CarrierID { get; set; }

    [Required]
    [MaxLength(255, ErrorMessage = "Carrier Name must be 255 characters or less")]
    [Display(Name = "Carrier Name")]
    public string Name { get; set; }
    ....
    ....
    [Required]
    [ConcurrencyCheck]
    [Timestamp]
    public byte[] RowVersion { get; set; }
}

我的 Carrier 类具有以下属性:

[MetadataType(typeof(CarrierMetadata))]
public partial class Carrier
{
}

但它仍然不起作用。

4

0 回答 0