简体   繁体   English

删除多对多关系中的实体

[英]Delete Entities in Many to Many Relationship

I have the following EF 6.1 entities: 我有以下EF 6.1实体:

public class Post {
  public Int32 Id { get; set; }
  public virtual ICollection<PostLocalized> PostsLocalized { get; set; }
}

public class PostLocalized {
  public Int32 Id { get; set; }
  public String Culture { get; set; }
  public virtual Post Post { get; set; }
  public virtual ICollection<Pack> Packs { get; set; }
}

public class Pack {
  public Int32 Id { get; set; }
  public virtual ICollection<File> Files { get; set; }
}

public class File {
  public Int32 Id { get; set; }
  public virtual Pack Pack { get; set; }
}

So in SQL I have the following tables, with Cascade on Delete: 因此,在SQL中,我具有下表,其中“删除”为Cascade:

Post, PostLocalizaded, PostLocalized_Packs, Packs, Packs_Files, Files

NOTE: PostLocalized_Packs and Packs_Files are Many to Many relationship tables. 注意:PostLocalized_Packs和Packs_Files是多对多关系表。

When I delete a Post its PostLocalized records are deleted and the PostLocalized_Packs to. 当我删除一个帖子时,其PostLocalized记录将被删除,并将PostLocalized_Packs删除。

But the Packs, Packs_Files and Files are not ... I know Packs_Files will be deleted the moment I delete the Packs. 但是Packs,Packs_Files和Files不是...我知道Packs_Files将在我删除Packs的同时被删除。

QUESTION

How can I, given a Post and its PostLocalized delete all PAcks and Files associated with them without loading everything, specially the Files. 给定一个帖子及其PostLocalized,如何在不加载所有内容的情况下删除所有与它们相关的包和文件,特别是文件。

Thank You, Miguel 谢谢米格尔

Not quite an answer and not quite a comment either but... 也不是一个答案,也不是一个评论,但是...

You said that if you delete Pack then the Pack_Files and Files are deleted also, because of cascading delete. 您说过,如果删除Pack,则由于级联删除,Pack_Files和Files也将被删除。

Consider this example: 考虑以下示例:

You delete a Post -> all PostLocalized and PostLocalized_Packs are deleted related to the post 您删除帖子->删除与该帖子相关的所有PostLocalized和PostLocalized_Packs

This should then also delete the Packs related to the PostLocalized_Packs, right? 然后,这还应该删除与PostLocalized_Packs相关的Pack,对吗? Well it does and you have cascading delete from Pack -> PostLocalized_Packs. 很好,您可以从Pack-> PostLocalized_Packs中级联删除。 It would then start deleting all those PostLocalized_Packs related to the Pack(s) deleted with the cascading delete. 然后,它将开始删除所有与级联删除操作删除的包相关的PostLocalized_Packs。

And afterwards it would be going after the Posts which were related to the PostLocalized_Packs which were deleted with the cascading delete from the Packs side. 然后,它将继续处理与PostLocalized_Packs相关的帖子,这些帖子已从Packs侧级联删除。

Is your model modeled properly if deleting a post should delete all the packs as well? 如果删除帖子应同时删除所有包,您的模型是否正确建模?

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

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