I have a [Product]-1-*-[Component]. So when I delete a product, I want EF to delete all its components. In the designer I select the relationship and set the OnDelete property for End1 to Cascade, whose multiplicity is 0..1 – this generates something like:
ALTER TABLE [dbo].[Components]
ADD CONSTRAINT [FK_ProductComponent]
FOREIGN KEY ([Product_Id])
REFERENCES [dbo].[Products]
([Id])
ON DELETE CASCADE ON UPDATE NO ACTION;
which to me, means that when a component is deleted, the deletion should cascade and the related product should also be automatically deleted.
This is backwards from what I wanted. So I edited the property for End2 to Cascade (End1 gets reset to None) but when trying to save the model I get:
Error 28 Running transformation: End 'Text' on relationship
'EF.ProductComponent' cannot have operation specified since its multiplicity
is '*'. Operations cannot be specified on ends with multiplicity '*'. C:\Users
\me\Documents\Visual Studio 2010\Projects\X\Website\Models\EF.edmx
clearly I don’t understand this. it IS possible to do what I want, no? how?
Here is a good link that explains it in detail. But in short, EF isn’t very dependable in handling cascade deletes. You should set this up on the database end manually because EF probably didn’t.
http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx
And the second part of my answer is….. A cascade delete works such that if you delete a parent… all the children get deleted as well. Deleting a child has no effect on the parent whether you have cascade delete on or not.
And yes, the ON DELETE CASCADE should appear on the child table’s foreign key constraint.
Another link with good info: http://msdn.microsoft.com/en-us/library/ms186973.aspx