I know a lot has been written about that subject, so let me say first I carefully read first 2 pages on Google about this topic.
Many suggest to put DeleteBehavior.Restrict
, and dotnet ef database update
does not complain anymore. Problem solved until you try to delete record.
I have exactly the same problem as in this SO question, which is duplicate to this one. Proposed solution in second link is:
You are expected to break the cycle. You can do that by turning off cascade delete (by including WillCascadeOnDelete(false) in the respective relationship configuration) for at least one of the relationships customers->payments or customers->billingCenters.
This is what I research so far.
Now let's get to the problem (again). I have diamond relationship:
All ForeignKeys must be not null
. So setting one foreign key to allow null is not an option. I would also like that user can delete:
- Parameter-Value-Parts -> No problem here
- Parameter-Values -> No problem here
OnDelete(DeleteBehavior.Cascade)
- Parameter-Parts -> No problem here
OnDelete(DeleteBehavior.Cascade)
- Parameters -> Big problem
To follow answer on this SO question, I could break cycle between Parameter-Parts
and Parameter-Value-Parts
with OnDelete(DeleteBehavior.Restrict)
.
This would allow me to delete Parameter
, but now I can not delete Parameter-Parts
.
I could manually delete all Parameter-Value-Parts
before deleting Parameter-Parts
, but I would like to avoid manually deletion. Is that possible?
I also read on SO, that all cascade delete
should be avoided in application and developer should manually take care of deleting dependent table rows before row deletion. What is consider best practice?
Delete cascade seems easy solution, but I am not searching for an easy solution but the right one. The one that would scale easily on large data model in large application.