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
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