简体   繁体   中英

Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising

I am trying to handle concurrency using entity framework 6. My entity looks like below;

public partial class Group : BaseEntity
{
    public byte[] RowVersion { get; set; }
    ...............
}

Mapping

public partial class GroupMap : EntityTypeConfiguration<Group>
{
    public GroupMap()
    {
        this.ToTable("CRM_Sales_Group");
        this.HasKey(c => c.Id);
        //tried like this too -> this.Property(e => e.RowVersion).IsRowVersion();
        this.Property(e => e.RowVersion).IsConcurrencyToken().IsRowVersion();
    }

The generic repository update method doesn't modify the state, it just validates and then i call the savechanges();

            public virtual void Update(T entity)
            {
                try
                {
                    if (entity == null)
                        throw new ArgumentNullException("entity");
                }
                catch (DbEntityValidationException dbEx)
                {
                    var msg = string.Empty;

                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                        foreach (var validationError in validationErrors.ValidationErrors)
                            msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, 

validationError.ErrorMessage);

                    var fail = new Exception(msg, dbEx);
                    //Debug.WriteLine(fail.Message, fail);
                    throw fail;
                }
            }
        }
       _groupRepository.Update(group);

And in the Razor view;

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

Still no luck at catching it in controller

        public ActionResult Edit(GroupModel model)
        {

            if (ModelState.IsValid)
            {
                try
                {
                    group = model.ToEntity(group);
                    _groupService.UpdateGroup(group);

                    ContainerResolver.Resolve<IDbContext>().SaveChanges();

                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var entry = ex.Entries.Single();
                    throw;
                }
            }
            return View(model);
        }

What am i really missing ?

Update 1

Well i am trying to follow the official documentation. http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application . I tried to edit the same record in two different tabs of the browser and updated the one record and the same record in another tab update. The rowversion value is comming different but the exception isn't firing.

Actually i was missing

db.Entry(departmentToUpdate).OriginalValues["RowVersion"] = rowVersion;

Now its working :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM