Linq to sql - delete some related records

拥有回忆 提交于 2019-12-10 14:56:04

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/2509842/linq-to-sql-delete-some-related-records

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!