zzzprojects / GraphDiff

GraphDiff is a library that allows the automatic update of a detached graph using Entity Framework code first.
https://entityframework-graphdiff.net/overview
MIT License
333 stars 101 forks source link

RowVersion failed optimistic concurrency #79

Closed poooria closed 10 years ago

poooria commented 10 years ago

Hi everybody I have these classes public abstract class BaseEntity { [Key] public int Id { get; set; } [Timestamp] public byte[] RowVersion { set; get; } } public class Calendar : BaseEntity { public string Title {get; set;} public virtual ICollection Days { get; set; } } public class Day : BaseEntity { public int DayNo { get; set; } public string Title { get; set; } }

and i used generic repository with unit of work pattern so i have DbContext class like this: public class BaseDbContext : DbContext, IUnitOfWork { public virtual void UpdateWithCollection(T entity, Expression<Func<IUpdateConfiguration, object>> mapping) where T : BaseEntity ,new() { this.UpdateGraph(entity, mapping); } } and in my controller class i used Automapper to map the viewmodels to models like this public MyActionResult Edit(CalendarViewModel calendar) { if (ModelState.IsValid) { var ent = Mapper.Map<CalendarViewModel, Calendar>(calendar); //var ent = _calendarService.GetById(calendar.Id, true);// second parameter tell the service to fetch the object in detach state _uow.UpdateWithCollection(ent, x => x.OwnedCollection(p => p.Days)); _uow.SaveChanges(); } return new MyActionResult(); } UpdateGraph is throw exception 'RowVersion failed optimistic concurrency'. when i remove the mapper line and get object like commented in controller it is working fine

refactorthis commented 10 years ago

Does your viewModel contain the rowversion or timestamp? Looks to me like it doesn't or is not mapping this value back to the entity you are updating.

refactorthis commented 10 years ago

Closing as no response - please comment if need further help