Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascading delete in Entity Framework

So I have two tables, Invoices and InvoiceItems. When I delete an Invoice, I'd like all the related InvoiceItems to be deleted as well.

I updated the relationship in SQL Server to do a cascading delete when I delete an Invoice. Entity Framework didn't recognize that change, however, but I've read that I need to manually update my EDMX to do the cascading delete.

Well in the design view of my EDMX, I clicked on the relationship between the two tables, and checked the properties to try and set my cascading delete

enter image description here

As you can see, there are two OnDelete properties: End1 OnDelete and End2 OnDelete

Which one do I need to set to Cascade?

like image 464
Steven Avatar asked Mar 10 '12 22:03

Steven


2 Answers

I was initially confused about this as well, and the reason was that I wasn't sure of what whether it was the field Navigation Property or the field Role Name that described what object an end represented, making an answer as Devidigitals ineffective.

And the answer to that is that a end is described by the field role name.

So if we know that we want to delete all InvoiceItems for a Invoice when the invoice in question gets deleted, we probably know that we should set the OnDelete for the Invoice, and to find the specific end that represents the invoice in the relation, search for the end where the role name is "Invoice". In the above example, that is End1.

This might be obvious if you know it, but not if you don't.

like image 132
Alex Avatar answered Oct 22 '22 23:10

Alex


If End1 is the principal of the relationship (I.e your invoice which has invoice items) then it makes sense for it to cascade deletes.

like image 34
devdigital Avatar answered Oct 22 '22 22:10

devdigital