Entity framework 未首先在数据库中引发DbUpdateConcurrencyException

Entity framework 未首先在数据库中引发DbUpdateConcurrencyException,entity-framework,asp.net-mvc-4,Entity Framework,Asp.net Mvc 4,我目前正在学习本教程的并发处理 但是,在我的案例中没有引发DbUpdateConcurrencyException。我在EF5.0中使用数据库优先的方法 我的载体对象与地址和联系人对象的关系为0..1 我已在Carrier表中创建了RowVersion字段 我的代码如下: public Carrier UpdateCarrier(Carrier carrier) { try { var entry = ariesConfigCon

我目前正在学习本教程的并发处理

但是,在我的案例中没有引发DbUpdateConcurrencyException。我在EF5.0中使用数据库优先的方法 我的载体对象与地址和联系人对象的关系为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; }
}
“我的承运人”类别具有以下属性:

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

但它仍然不起作用

您是否尝试使用2个上下文编写单元测试来精确创建冲突场景?@HenkHolterman没有尝试使用单元测试,而是使用浏览器进行测试[如教程链接中所述]。如果您首先使用数据库,是否在设计器中的时间戳的属性面板中设置了Concurrency Mode=Fixed?@bmused-是的,我有。
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; }
}
[MetadataType(typeof(CarrierMetadata))]
public partial class Carrier
{
}