简体   繁体   English

Linq to SQL在删除父级时删除子级及其关系

[英]Linq to SQL Delete child and its relationships while Deleting Parent

The relationships in my case Photo -PhotoId PhotoGroupsMap - MapingId(PK), PhotoId(FK with Photo) PhotoShare - ShareId(PK),PhotoId(FK with Photo) PhotoGroupComment - MapingId (FK with PhotoGroupsMap) 我的案例中的关系Photo -PhotoId PhotoGroupsMap-MapingId(PK),PhotoId(FK与照片)PhotoShare-ShareId(PK),PhotoId(FK与照片)PhotoGroupComment-MapingId(FK与PhotoGroupsMap)

And i am using LinqSql for deletion and i want to Delete a Photo Object and i used the code 我正在使用LinqSql进行删除,我想删除一个照片对象,并且我使用了代码

  Photoset _item = (from item in dbContext.Photos
                              where ....
                              select item).SingleOrDefault();

            //remove all Photo Share Entries
            dbContext.PhotosetShareMaps.DeleteAllOnSubmit(_item.PhotoShareMaps);
           //remove all Photo Group entries
            dbContext.PhotoGroups.DeleteAllOnSubmit(_item.PhotoGroups);

But how can i delete all Comments Associated with Groups? 但是如何删除与组相关的所有评论? Since in this case if am trying to Delet Some Photo which is in some groups and the group contains some comment row , i am getting error (Since i am not removeing comments ). 因为在这种情况下,如果尝试删除某些组中的某些照片,并且该组中包含一些注释行,则我将收到错误消息(因为我没有删除注释)。 How can i remove that ? 我该如何删除?

尝试级联删除“注释”,同时删除“组”。要获取有关级联删除的更多信息,请参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM