删除 Cascade 而不显式引用所有依赖项

发布于 2024-12-11 13:29:53 字数 360 浏览 5 评论 0原文

我正在为 QA SQL Server 数据库构建一个小型 db-cleaner 应用程序。当然,我需要删除依赖于它们的表行。

T-SQL 级联能力非常有限,因此我尝试使用 NHibernate 来简化问题。但是,我找到的唯一方法是为每个依赖项创建一个集合 在要删除的对象中,并将其标记为cascade=delete。

这意味着创建很多很多集合(在 hbm 文件和 C# 对象中),我不需要这些集合用于任何其他目的。这使得该方法与仅使用 SQL 一样复杂。

我错过了什么吗?有没有更简单、更通用的方法来执行删除级联?

谢谢。

编辑:需要明确的是,我避免更改数据库中的外键,因为它是一个 QA 数据库,设计为与生产数据库相同。

I'm building a small db-cleaner app for a QA sql server database. Naturally, I need to delete table rows with dependencies on them.

T-SQL cascading abilities are very limited, so I've tried using NHibernate to simplify matters. But, the only way I found for this was to create a collection for each dependency
in the object-to-delete, and mark that as cascade=delete.

That means creating many, many collections (both in the hbm file and in the C# object) which I don't need for any other purpose. Which makes this method as complicated as just using SQL.

Am I missing something? Is there any easier, more generic way to perform delete-cascade?

Thanks.

EDIT: Just to be clear, I avoid changing the foreign keys in the DB because it's a QA DB, designed to be identical to the production DB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2024-12-18 13:29:53

最终我找到了一种通用的删除方法:

这个人写了一个递归 SP,它可以为你完成所有工作:

http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7

需要一点修改-ups(因为我的数据库使用模式)但工作起来就像一个魅力。

Eventually I found out a generic way to do the deletion:

This guy wrote a recursive SP which does all the work for you:

http://www.sqlteam.com/article/performing-a-cascade-delete-in-sql-server-7

Needed a little touch-ups (since my DB uses schemas) but works like a charm.

北方。的韩爷 2024-12-18 13:29:53

我想您在数据库中的相关表之间定义了外键?
您可以在外键级别指定删除父记录时相关记录应发生的情况。
查看 MSDN 了解级联选项以及如何定义它们:

级联 FK 约束

外键约束

I suppose you have foreign keys defined between related tables in your database ?
You can specify at the foreign key level what should happen with related records when a parent record is being removed.
Check out MSDN for the cascading options, and how to define them:

Cascading FK constraints

Foreign Key Constraints

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文