I use Entity Framework 4 and I have parent - child relation with "Cascade Delete" set.
So i would expect when i remove a child from the parent that the child is deleted when i call SaveChanges().
cuRepository.Attach(_controlUnit);
foreach (var recipe in recipes) {
_controlUnit.Recipes.Remove(recipe);
//repository.DeleteObject(recipe);
}
Instead i get an error:
System.InvalidOperationException occurred 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.
When I explicitly delete the children (see commented line), all is fine. What am I missing?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…