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

830 views Asked by At

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.

1

There are 1 answers

0
Idrees Khan On BEST ANSWER

Actually i was missing

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

Now its working :)