How does WillCascadeOnDelete in Entity Framework work?

cascade entity-framework

Question

As I understand, if I delete a parent row, its children should be deleted if I turn on cascade on delete. However, from my testing, it doesn't seem to work at all. No matter if I set WillCascaseOnDelete to true or false, it simply sets the foreign key of its children to null. This is causing another problem that I have to set the foreign key nullable, otherwise, SaveChange will throw exception. Is this a defect or desired behavior?

1
16
11/20/2018 11:02:06 AM

Accepted Answer

This is because your foreign keys (child) are nullable. By default, when deleting parent, if the foreign key on the relationship is nullable EF will delete the parent and set the foreign key to null. If the foreign key is NOT NULL it will delete the child (the behaviour you're looking for?).

You can alter this default behaviour here

10
9/25/2013 4:05:12 PM

Popular Answer

Make sure on the Foreign Key Relationship window in SQL Server, you have selected Cascade as Delete rule.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow