// Rollback changes
switch (entry.State)
{
case EntityState.Added:
entry.State = EntityState.Detached;
break;
case EntityState.Modified:
entry.CurrentValues.SetValues(entry.OriginalValues);
entry.State = EntityState.Unchanged;
break;
case EntityState.Deleted:
entry.State = EntityState.Unchanged;
break;
}
This code is used after EF6 SaveChanges()
exception
My scanrio, I insert some values and delete old one then I get an exception. My data should be preserved because of rollback and web site should work normally. But I get this exception in "EntityState.Unchanged" in sector "EntityState.Modified:"
Message=The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. Source=EntityFramework
Why I can not change DB context back to previous state?
EDIT:
How I delete dependencies?
workReport.sepapayments.Remove(sepaPayment);
workReportAccountSettlement.workreports.Remove(workReport);