DbUpdateConcurrencyException未引发数据库优先

时间:2013-05-15 20:03:09

标签: entity-framework asp.net-mvc-4

我目前正在阅读本教程中的并发处理。

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&的关系为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
{
}

但它仍然无效。

0 个答案:

没有答案