如何删除与特定实体相关的所有实体? [英] How do I delete all the entities related to a specific entity?

查看:93
本文介绍了如何删除与特定实体相关的所有实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework 4.0,并且要删除实体上导航属性所指向的所有实体。我该怎么做?

I am using Entity Framework 4.0 and I want to delete all the entities that are pointed to by the navigation property on an entity. How can I do this?

推荐答案

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

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.

这篇关于如何删除与特定实体相关的所有实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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