In my MS SQL Server 2008 database, I have a self-billing table with categories for the hierarchy (ID and ParentID). The table has its own foreign key constraint and a "Instead of deleting" trigger to delete a full node with its children.
For data management, I use Entity Framework (4.3), with a model generated from a database with self-monitoring objects and an ObjectContext
(generated by the VS template). EDM also has a self-esteem association for the category object.
I ran into a problem while trying to delete any parent row containing at least one child row.
After the call:
Entity.MarkAsDeleted(); Context.SaveChanges();
In SQL Server Profiler, I see that EF first generates an update statement to set the ParentID of the child rows to zero, and then deletes the parent row! Of course, the cascading rule in the database does not work, and the child nodes remain in both the EF context and the DB.
I tried to set the association rule "On delete" to "Cascade" and "None", but that doesn't make sense ...
How can I cascade deletion in a self-monitoring table using EF, or at least how to prevent EF from updating parent child row identifiers?
PS: here I found the exact same unanswered problem (MSDN)
source share