实体框架 - 按ID删除相关记录 [英] Entity framework - remove related records by id

查看:143
本文介绍了实体框架 - 按ID删除相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好。假设我有结构:


学校 - >学生 - > StudentParents< - 父母 - >地址

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.

如果给予school_Id = 5,我想删除这所学校和所有相关记录。
如何在Entity Framework 4中轻松实现?

If given school_Id =5, I want to remove this school and all related records. How to do this easily in Entity Framework 4?

推荐答案

您的问题的答案与此问题


您正在尝试在错误的层面解决问题。您需要
重新考虑您的数据库设计,特别是如何维护
引用完整性。

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.

您需要设置CASCADE DELETE外键并在您的实体模型中反映
。然后,数据库将在删除
实体时,进行必要的
更改以维护引用完整性。

You need to set the "CASCADE DELETE"s of the foreign keys and reflect that in your Entity Model. Then the database will make the necessary changes to maintain the referential integrity when you delete that entity.

这篇关于实体框架 - 按ID删除相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆