I'm trying to design a rollback changes system. I'm tracking old record values when a record id modified or deleted and then I'm using that values to re-insert it when user choose to rollback it...
editions rollback are working as expected.
deletions are not... because when I attach the entity to DBContext it has the old Id but when I execute DbContext.SaveChanges(); the Id is replaced per a new one(Auto Incremented)
Here is the sample code:
var model = JsonConvert.DeserializeObject(oldValues.OldData, type);
DbSet mySet = this.Set(type);
mySet.Attach(model);
this.Entry(model).State = oldValues.AuditType == 0 ? EntityState.Modified : EntityState.Added;
base.SaveChanges();
into model.Id I can see the correct Id before save but after it EF changes it per a new one.
is there anyway to set temporally to keep the current Id instead of generate a new one?
I tried many solutions but none of them works.
Thanks