EF 4.1: Removing a child from a collection does not delete it - why?

I have some error: EF 4: Removing a child from a collection does not delete it - why?

when I remove the child from the parent, when the child is deleted, when I call SaveChanges() , it gives the following error message:

The operation failed: the relation cannot be changed because one or more properties of the foreign key are not NULL. When a change in relationship occurs, the corresponding property of the foreign key is set to zero. If the foreign key does not support null values, a new relationship must be defined, another nonzero value must be assigned to the foreign key property, or an object not associated with it must be deleted.

But with DbContext and EF 4.1, "context.DeleteObject (recipe)" does not exist.

Any suggestion?

[EDIT]

  public void UpdateWithAttributes(Model model, IEnumerable<Entity> entities) { var modelOriginal = this.unitOfWork.Model.GetById(model.IModel); this.unitOfWork.Context.Entry(modelOriginal).CurrentValues.SetValues(model); UpdateEntityAttributeAssociations(modelOriginal, entities); this.unitOfWork.Commit(); } public void UpdateEntityAttributeAssociations(Model model, IEnumerable<Entity> current) { unitOfWork.Context.Entry(model).Collection(m => m.Entities).Load(); ICollection<Entity> original = model.Entities; // perhaps .ToList() necessary // delete if (original != null) { List<Entity> toDelete = GetToDelete(original, current); foreach (Entity originalEntityToDelete in toDelete) { unitOfWork.Context.Entity.Remove(originalEntityToDelete); } } // add, update if (current != null) { foreach (Entity currentEntity in current) { // No need to set the UpdatedWhen. The trigger on the table will handle that. if (original.Where(originalEntity => originalEntity.IEntity == currentEntity.IEntity).FirstOrDefault() == null) { model.Entities.Add(currentEntity); } } } } 
+5
source share
2 answers

I assume that you are again adding what you just deleted, since deleting from DbSet also removes the entity from the original child collection from the inside. Then in the second loop you add the object again. You can try to catch this situation like this:

 public void UpdateEntityAttributeAssociations(Model model, IEnumerable<Entity> current) { unitOfWork.Context.Entry(model).Collection(m => m.Entities).Load(); ICollection<Entity> original = model.Entities; // perhaps .ToList() necessary // delete List<Entity> toDelete = null; if (original != null) { toDelete = GetToDelete(original, current); foreach (Entity originalEntityToDelete in toDelete) { unitOfWork.Context.Entity.Remove(originalEntityToDelete); } } // add, update if (current != null) { foreach (Entity currentEntity in current) { if (toDelete == null || !toDelete.Contains(currentEntity)) { if (original.Where(originalEntity => originalEntity.IEntity == currentEntity.IEntity).FirstOrDefault() == null) { model.Entity.Add(currentEntity); } } } } } 

I also replaced your first line with explicit loading of the child collection, as your procedure for manually requesting child entities seems unfamiliar to me, but I don't think this is a problem (but I don't know).

Edit

I'm not sure my code above helps remove the exception you had. I do not know what will happen if you add an object to a collection that is already in the Deleted state, and if this can cause this exception.

If the code does not help, you can check if you have a problem without a second loop (Insert). And what exactly does GetToDelete look like? And what do your Model and Entity classes and their relationships look like? And is model.Entity really a different collection than model.Entities (or is it a typo)?

+1
source

You need to call:

 context.Recipes.Remove(recipe); 

Where is the Recipes DbSet<Recipe> .

+2
source

All Articles