26

Does anyone know how to delete an object and all of its related entities inside of EF without manually traversing the object graph and deleting each one?

For example, I've got SalesOrder and SalesOrderDetails with a 1:N relationship between them. When I delete a SalesOrder, I want all SalesOrderDetails to be deleted automatically.

Is this possible in EF?

Bohemian
  • 412,405
  • 93
  • 575
  • 722
LPCRoy
  • 935
  • 2
  • 12
  • 19

2 Answers2

26

You should not be doing this in the Entity Framework. All popular relational databases support ON CASCADE DELETE on foreign keys which is a lot more efficient as well. I suggest you just go with that.

aleemb
  • 31,265
  • 19
  • 98
  • 114
  • 7
    Yeap. If you have cascade delete on your relationships in database, and you bring that into an EF model, the EF will in fact delete dependent entities in memory to attempt to keep the in memory object graph in sync with the database. But you shouldn't rely on EF deleting related all related objects that is the job of the database. – Alex James Jun 02 '09 at 03:43
  • 2
    Thanks Alex, I was worried that CASCADE DELETE would mess up the in memory state but if EF is keeping it up to date then this works! – LPCRoy Jun 03 '09 at 00:08
  • this is very good solution because you don't have to worry about other related entities. Just set cascade delete on DB - everything will be clean and simple. – torpederos Dec 16 '11 at 11:42
  • 7
    Not a big fan of CASCADE DELETE. Deleting records when they are not mentioned in a delete operation scares me. It may be a little more work, but setting up appropriate referential constraints and explicitly deleting associated data is safer. CASCADE DELETE can make it hard to track down bugs and possibly cause inadvertent data loss if you don't remember you have it turned on (or another developer you are working with doesn't know about it). – Justin Aug 08 '12 at 13:37
  • I think the EF as ORM should use SQL procedure or trigger which deletes all relations. By deleting relation I mean finding all records pointing to the record, which has to be deleted, and set the fields holding id (pointing to the record) to null (if it is set to ALLOW NULL as it takes place with relation ONE OR ZERO TO MANY) – Bronek Nov 01 '12 at 15:15
  • Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. So actually this answer is not fool proof. – John May 21 '15 at 19:06
7

in this article, Alex Jamese (who post his answer), has a complete article on the topic.

Link

The EF is responsible for the correctness of the ObjectContext after SaveChanges(). So the EF attempts to synchronize the ObjectContext, with the expected database state after the expected cascade in the database. A tell tale sign of this is that if you open up something like SqlProfiler, you will notice the EF issuing DELETE requests for dependent entities that it knows about (i.e. that are loaded in the ObjectContext) when a principal is deleted. Essentially what is happening here is that the Entity Framework expects that deleting the principal in the database, will delete all it’s dependents in the database. So it issues, what should be, a redundant DELETE to request itself so the dependents already loaded are deleted from the ObjectContext. The key thing to note is that the EF does not retrieve all the dependent entities and issue deletes for them: It only deletes dependents that are already in memory.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Marco Staffoli
  • 2,475
  • 2
  • 27
  • 29