I have a some error : EF 4: Removing child object from collection does not delete it - why?
when i remove a child from the parent that the child is deleted when i call SaveChanges()
, it gives the follow error 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.
But with DbContext and EF 4.1, the "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);
}
}
}
}