2
// 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);
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • You need to remember previous values of your entites somehow if you need to undo changes in the given DBContext. But unless you change the data int the database using `SaveChanges()` you can dispose the DBContext and create a new DBContext from database values. – Vojtěch Dohnal May 05 '16 at 08:28
  • I can not so simply dispose it. I am using Ioc.var context = unitOfWork.Context as DbContext; – senzacionale May 05 '16 at 08:39

2 Answers2

1

the data you are trying to remove have dependent child objects and you have foreign key relation ship setup. therefore you can not delete/add.

Have a look on items you are adding and removing and ensure you have valid entities.

I would split your implementation into two

1st

delete existing items (ensure the dependent child items are removed before you remove parent or use cascade) - if you are using cascade be careful what are you deleting

2nd

add new items once deleting works

cpoDesign
  • 8,953
  • 13
  • 62
  • 106
  • That I can do, but first problem here is that I can not rollback my data. I updated my question how I delete dependencies – senzacionale May 05 '16 at 08:14
1

Instead of removing the entities from collections

workReport.sepapayments.Remove(sepaPayment);

workReportAccountSettlement.workreports.Remove(workReport);

try to delete them

myDBContext.Entry(sepaPayment).State = System.Data.Entity.EntityState.Deleted;  

myDBContext.Entry(workReport).State = System.Data.Entity.EntityState.Deleted;  

Or you can

myDBContext.ObjectContext.DeleteObject(sepaPayment);

See also this answer. The reason you get this exception is because FK is non-nullable, the UML association between those entites (classes) is a composition, so you need either completely delete the entity or change the FK of the related entity first.

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105