Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to sql - delete some related records

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?

like image 386
Oskar Kjellin Avatar asked Mar 24 '10 17:03

Oskar Kjellin


People also ask

How do I delete a record in LINQ query?

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.

Is LINQ to SQL obsolete?

LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.

How do I delete a specific record in a table?

The DELETE Statement in SQL is used to delete existing records from a table. We can delete a single record or multiple records depending on the condition we specify in the WHERE clause. DELETE FROM table_name WHERE some_condition; table_name: name of the table some_condition: condition to choose particular record.


1 Answers

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.

like image 191
bruno conde Avatar answered Oct 31 '22 01:10

bruno conde