Rahma
Rahma

Reputation: 255

How to do a delete on cascade using linq to entities?

I'm using the Northwind database to learn linq to entities and the entity framework. I'm trying to delete a Supplier entity using its ID. But in order to do that I have to delete all the entities related to it. Is there a feature in Entity framework that allows me to do that?

Upvotes: 2

Views: 2552

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

Entity framework's cascade delete ability is dependent on cascade delete correctly set on the relation in the database. Cascade delete in EF works this way:

  • You must configure cascade delete in database so that child entities which are not loaded to the context are property deleted before parent entity
  • You must configure cascade delete in EF so that child entities which are loaded to the context are properly deleted before parent entity

Upvotes: 3

Related Questions