When deleting an entity, I get the following 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.
Fair enough. However, I have many many relationships and I don’t know which one causes the error.
Is there a way to get the name of the relationship that causes the problem?
Unfortunately no without quite complex exploration of
ObjectStateManager. This is quite annoying problem because the exception is too generic without any additional information. I think it is a nice request for improvement and with EF as and open source it should really be possible to improve the quality of the exception.You will have to go through your modification code and check entities you are trying to delete. There will be some related dependent entity which is not deleted and hangs the deletion of the parent entity. Other approach recommended for EF is to use cascade deletes.