简体   繁体   English

实体框架 - 按ID删除相关记录

[英]Entity framework - remove related records by id

Okay. 好的。 assume I have structure: 假设我有结构:

School -> students -> StudentParents <- parents -> address 学校 - >学生 - >学生父母< - 父母 - >地址

School can have many students, students can be relatives and have the same set of parents (may-to-many). 学校可以有很多学生,学生可以是亲戚,也有同一套父母(可能是多对)。 Each parent can have multiple addresses. 每个父母可以有多个地址。
Assume that students who have the same set of parents cannot study in different schools. 假设拥有相同父母的学生不能在不同的学校学习。

If given school_Id =5, I want to remove this school and all related records. 如果给出school_Id = 5,我想删除这所学校和所有相关记录。 How to do this easily in Entity Framework 4? 如何在Entity Framework 4中轻松完成此操作?

Answer for your question would be same as this question . 您的问题的答案与此问题相同。

You are trying to solve the problem in the wrong layer. 您正试图在错误的层中解决问题。 You need to reconsider your database design specially how you maintain the referential integrity. 您需要重新考虑数据库设计,特别是如何维护参照完整性。

You need to set the "CASCADE DELETE"s of the foreign keys and reflect that in your Entity Model. 您需要设置外键的“CASCADE DELETE”并在实体模型中反映出来。 Then the database will make the necessary changes to maintain the referential integrity when you delete that entity. 然后,当您删除该实体时,数据库将进行必要的更改以维护参照完整性。

Entity framework cannot delete data from database that is not instantiated as object in memory. 实体框架无法从未在内存中实例化为对象的数据库中删除数据。 This means you would need to load school data, all students data, all students parent data and so on, and then you would need to manually delete all the data. 这意味着您需要加载学校数据,所有学生数据,所有学生家长数据等,然后您需要手动删除所有数据。

This seems like a lot of work to do, so you may want to take another approach to this problem - delete all this data using stored procedure on database that is mapped to ObjectContext , this would perform better since you would not need to get all the data into memory. 这似乎要做很多工作,所以你可能想采取另一种方法解决这个问题 - 使用映射到ObjectContext数据库上的存储过程删除所有这些数据,这样做会更好,因为你不需要得到所有的数据到内存中。

But this also seems troublesome. 但这似乎也很麻烦。 The best approach would be to create Cascade delete constrain on database and map it also in entity framework's model. 最好的方法是在数据库上创建Cascade delete约束并将其映射到实体框架的模型中。 This has two advantages - you would need to only load school data and after it is deleted from model, it would be deleted from database and cascade delete would remove all referencing data. 这有两个好处 - 您只需要加载学校数据,在从模型中删除后,它将从数据库中删除,级联删除将删除所有引用数据。 But if you have school and students data already in memory, EF will take care of marking those objects from memory as deleted, which will make your data consistent with database state. 但是如果您已将学校和学生数据存储在内存中, EF将负责将内存中的这些对象标记为已删除,这将使您的数据与数据库状态保持一致。

The best resolution to this problem depends on whether you may or may not modify database. 解决此问题的最佳方法取决于您是否可以修改数据库。 If you can - go for cascade delete . 如果可以 - 去cascade delete If you cannot - I would recommend stored procedure approach as better performing (assuming performance is an issue and there is lots of students, parents etc. in database). 如果你不能 - 我会建议stored procedure方法表现更好(假设性能是一个问题,数据库中有很多学生,家长等)。

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

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