PierrickM
PierrickM

Reputation: 676

How to delete related rows in a many to many relationship (SQL Server 2008 + Entity Framework 4)

For example, let's say that I have this 3 tables :

Table Customers:
CustomerID

Table Addresses:
AddressId

Table Customers_Addresses
CustomerID
AddressID

On SQL Server Management Studio, for the two FK I put "Delete Rule" and "Update Rule" to "Cascade".

So, when i delete a customer, all rows in "Customers_Addresses" that reference the CustomerID are deleted. Now, if I want that SQL Server 2008 also delete all rows in the "Addresses" table that are related to the customer i just deleted, what can i do ?

I read that i can load all related "Addresses" in my Customer object and call "DeleteObject" for all instances of Addresses. I'm not satisfied of this solution, i prefer a solution on SQL Server 2008, if it exists.

Thanks !

Upvotes: 1

Views: 931

Answers (1)

aF.
aF.

Reputation: 66717

Do a on delete trigger in table Customers_Addresses that will delete the info from table Addresses.

Here is a full example!

Upvotes: 2

Related Questions