简体   繁体   中英

Delete Entities in Many to Many Relationship

I have the following EF 6.1 entities:

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:

Post, PostLocalizaded, PostLocalized_Packs, Packs, Packs_Files, Files

NOTE: PostLocalized_Packs and Packs_Files are Many to Many relationship tables.

When I delete a Post its PostLocalized records are deleted and the PostLocalized_Packs to.

But the Packs, Packs_Files and Files are not ... I know Packs_Files will be deleted the moment I delete the 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.

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.

Consider this example:

You delete a Post -> all PostLocalized and PostLocalized_Packs are deleted related to the post

This should then also delete the Packs related to the PostLocalized_Packs, right? Well it does and you have cascading delete from Pack -> PostLocalized_Packs. It would then start deleting all those PostLocalized_Packs related to the Pack(s) deleted with the cascading delete.

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.

Is your model modeled properly if deleting a post should delete all the packs as well?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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