I know how to use and the effect of Cascade Delete in Entity Framework and generally do not use by default in my projects. However, I am not sure if deleting all of the child records after deleting their parent record is good behavior or not. Is there any need to keep child records after its parent deleted. Is WillCascadeOnDelete(false)
be used only to protect child records to be deleted by mistake? Or is there some other situations that requires to use WillCascadeOnDelete(false)
?
Asked
Active
Viewed 805 times
1
1 Answers
4
The answer is: "It depends on your business"
For example, you have two table: Category and Product with one to many relationship
If you are using WillCascadeOnDelete(true), it mean your products can't existed without a category.
Otherwise, it mean your products is not affected by category.
For more detail, you can try to read more about the different between Aggregation, Composition and Association at here, https://stackoverflow.com/a/31600062/7338888. Hope this is helpful to you.

Tan Sang
- 1,897
- 1
- 16
- 28
-
Thanks for reply. Even if If I do not use `WillCascadeOnDelete(false)` in EF, I encounter "*The DELETE statement conflicted with the REFERENCE constraint ...*" error when trying to delete a parent record. When looking Context class I realized that I used `modelBuilder.Conventions.Remove
();`. So, does it also prevent all of the table records from Cascade Deletion and cause the error above? – Sep 03 '19 at 10:33 -
Any help please? – Sep 03 '19 at 11:22
-
Hm, this is not error throw by EF, this is error throw under database, can you share you db structure? – Tan Sang Sep 03 '19 at 13:28