Entity Framework -- Dependent objects not deleted before principal object when cascade deleting composite key entities
I'm having an issue with cascade deletion of dependent objects with composite keys. Specifically, the emitted SQL instructions by EF tries to delete the principle entity before the dependent entity.
I have OnDelete="Cascade"
set in both the CSDL and SSDL of my EF model and am expli开发者_Go百科citly loading the dependent entities into memory so EF knows to handle them. I have this working correctly with another FK relationship and EF correctly deletes the children entities before deleting the principle.
However, it's not in this case and I suspect it's because the dependent entity has a Composite Key. A brief look at the schema:
Entity A Entity B Entity A_B
ID (PK) ID (PK) A_ID (PK, FK to A)
B_ID (PK, FK to B)
I want the entries of Entity A_B to be deleted when I delete Entity A.
I can look into the ObjectStateManager and see A's A_B entities loaded in memory and marked for deletion, yet EF emits a SQL command to delete A before deleting its A_Bs. This works fine when the dependent entity has its own PK, but seems to get tripped up when it's a Composite Key. It doesn't delete the children first even though I explicitly tell the EF's FK Association (both SSDL and CSDL) that Deletes should be Cascaded.
Again, I have both A and its A_Bs loaded in memory and the ObjectStateManager has them all marked to be deleted. I'm using POCOs with Proxy Creation enabled, though I explicitly load the child entities in memory via LoadProperty calls against the Context.
Has anyone seen this? Is the Composite Key really the issue or just a distraction? Why does EF handle the SQL commands in correct order in one case but not the other?
EDIT: I've read the documentation for both OnDelete (http://msdn.microsoft.com/en-us/library/cc716734.aspx) and Relationship Management (http://msdn.microsoft.com/en-us/library/ee373856.aspx). The section entitled "Considerations for Identifying and Non-identifying Relationships" seems to imply that what I'm wanting to do is possible, and is, indeed, expected. Could it perhaps be something within the EF provider assembly for the database? I'm working against an Informix database using IBM.Data.DB2 assembly and NOT against a SQL Server database.
(Further update: I have updated to IBM DB2 v9.7fp3a and EF Providers Beta Refresh -- http://www.ibm.com/developerworks/forums/thread.jspa?threadID=345634&tstart=0)
There may be some error that's being swallowed by the framework - it happens sometimes. I presume that there's some equivalent to Sql Profiler for DB2...I would profile the activity of the db and see what's going on there. If you don't see the call to delete at all, at least you've eliminated one possibility of the error.
精彩评论