I have following problem. I have typical master/detail UI scenario. I set main object (CommissionPlan) as a DataContext. Then I bind grid to CommissionPlanItems (this is collection of child items)
Everything works great on additions/updates. When I try to delete existing row – I get following EF error:
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.
I found bunch of recepies on how to handle this. But I’m not sure how to do it in my case since deletion happens auto-magically, via deleting row from a grid. What should I modify in this case?
Here is what I ended up doing to “clean up”. I will mark it as an answer, but I will appreciate if someone gives me better way to do this. Basically, I’m checking ObjectStateManager for modified entities (is there better way to see those?) and then if I see that modification involved setting parent to “null” – I know that this entity was “detached” from my graph and I go ahead and delete it.