开发者

Linq to sql - delete some related records

开发者 https://www.devze.com 2022-12-24 18:06 出处:网络
I´m using linq to sql and I have a lot of tables with foreign keys lea开发者_StackOverflowding to the UserId.

I´m using linq to sql and I have a lot of tables with foreign keys lea开发者_StackOverflowding 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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号