views:

160

answers:

2

Hey!

I´m using linq to sql and I have a lot of tables with foreign keys leading to the UserId.

Is it possible to have some of these foreign tables cleaned upon deletion.

For example I want the users profile (other table) to be deleted automatically with the user but not the users forum posts.

Is this possible or do I have to handle this with code?

A: 

I am not sure with code, but couldn't you set the Cascade on Delete option in SQL?

gmcalab
+5  A: 

I think this link is very usefull.

LINQ to SQL does not support or recognize cascade-delete operations. If you want to delete a row in a table that has constraints against it, you must complete either of the following tasks:

  • Set the ON DELETE CASCADE rule in the foreign-key constraint in the
    database.

  • Use your own code to first delete the child objects that prevent the parent object from being deleted.

bruno conde
Thanks for the response. Have not yet tried this, I've had my hands full with other stuff. I'll try this today and mark as answered if it works :)
Oskar Kjellin