2

I've been trying to find the answer to this question here. Several people seem to ask similar things, but I don't get the answers. I have an EF entity with a bunch of child entities (one-to-many relationship). I want to be able to delete the "parent" entity and have all the child entities deleted at the same time.

Some people mention "Cascade Delete" should be set on both EF model and database (Sql Server in my case). The problem is:

  1. I have absolutely no idea how to do this (seems to be implied in those answers that you should know, but sorry...)
  2. I have a feeling I've run into a similar problems before and found an answer somewhere that was simpler than setting this Cascade Delete. I may be wrong, maybe it is the only way, but if there is a simpler solution I'd like to know.

In either case, a clear example of how to get this working would be greatly appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anders
  • 12,556
  • 24
  • 104
  • 151

2 Answers2

5

In SQL Managment Studio go to your database and find the table where there should be a foreign key. Add a foreign key to the table pointing to the other table. I assume you know how to setup a foreign key. In the foreign key setup at the bottom of the dialog window you'll see a Delete property. Set it to Cascade. This will cause any dependent rows to be deleted whenever the parent row is deleted. Then go and update your data model in Visual Studio. Everything should be setup for you now.

Paul Mendoza
  • 5,709
  • 12
  • 53
  • 82
  • Great, I did it through the table designer in Visual Studio because I rarely use SQLMS and don't feel at home in it, but it worked fine from VS. Just a question though before I close this: I updated the model from the database like you said, but when I selected a relationship in the model designer and looked at its properties it said things like End1 OnDelete = None (instead of Cascade which I had expected). Again it works, but shouldn't this have been changed by the update model command? – Anders Feb 22 '11 at 22:28
  • @Anders Svensson Hmm... maybe it only changes it when you import the table fresh. Just change the OnDelete property to be cascade and you should be good. – Paul Mendoza Feb 22 '11 at 23:02
  • Ok, both End1 OnDelete and End2 OnDelete? (Both of those properties are there). – Anders Feb 22 '11 at 23:21
  • updating this answer: you set 'End1 OnDelete' if End1 == the table that has 1 record for many End2 records. – Alex Fairchild May 29 '13 at 16:40
0

Here is some relevant documentation on MSDN. Note though that there appears to be an error in the example. I received the following error from the EDMX designer when using this configuration.

Operations cannot be specified on ends with multiplicity '*'.

You should set the OnDelete property to Cascade for the end will be triggering deletes on the other end.

As an example, in a relationship involving customers and orders where you would like to have a customer's orders deleted along with the customer, you should set the OnDelete property for the Customer role to Cascade.

Note that only objects that have been loaded into the ObjectContext will be affected by a cascading delete. You will be relying on the cascading delete that you set in the database to look after any other records.

Scott Munro
  • 13,369
  • 3
  • 74
  • 80