使用 NHibernate 限制删除

发布于 2024-08-29 11:04:00 字数 346 浏览 3 评论 0原文

我正在使用 NHibernate(流畅)访问带有一堆表的旧第三方数据库,这些表不以任何显式方式相关。也就是说,子表确实有包含父表主键的parentID 列,但没有外键关系来确保这些关系。理想情况下,我想添加一些外键,但不能触及数据库模式。

我的应用程序工作正常,但我真的想强加一个引用完整性规则,该规则将禁止删除父对象(如果它们有子对象),即类似于“ON DELETE RESTRICT”但由 NHibernate 维护的东西。

任何关于如何解决这个问题的想法将不胜感激。我应该研究 IInterceptor 接口上的 OnDelete() 方法,还是有其他方法来解决这个问题?

当然,任何解决方案都会带来性能损失,但我可以忍受。

I'm using NHibernate (fluent) to access an old third-party database with a bunch of tables, that are not related in any explicit way. That is a child tables does have parentID columns which contains the primary key of the parent table, but there are no foreign key relations ensuring these relations. Ideally I would like to add some foreign keys, but cannot touch the database schema.

My application works fine, but I would really like impose a referential integrity rule that would prohibit deletion of parent objects if they have children, e.i. something similar 'ON DELETE RESTRICT' but maintained by NHibernate.

Any ideas on how to approach this would be appreciated. Should I look into the OnDelete() method on the IInterceptor interface, or are there other ways to solve this?

Of course any solution will come with a performance penalty, but I can live with that.

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

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

发布评论

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

评论(1

把昨日还给我 2024-09-05 11:04:00

我想不出在 NHibernate 中执行此操作的方法,因为这需要 NHibernate 对这些关系有一定的了解。我将使用 规范模式 在代码中处理此问题。例如(使用带有指向 Employee 对象的链接的 Company 对象):

public class CanDeleteCompanySpecification
{
    bool IsSatisfiedBy(Company candidate)
    {
        // Check for related Employee records by loading collection
        // or using COUNT(*).
        // Return true if there are no related records and the Company can be deleted.
        // Hope that no linked Employee records are created before the delete commits.
    }
}

I can't think of a way to do this in NHibernate because it would require that NHibernate have some knowledge of the relationships. I would handle this in code using the sepecification pattern. For example (using a Company object with links to Employee objects):

public class CanDeleteCompanySpecification
{
    bool IsSatisfiedBy(Company candidate)
    {
        // Check for related Employee records by loading collection
        // or using COUNT(*).
        // Return true if there are no related records and the Company can be deleted.
        // Hope that no linked Employee records are created before the delete commits.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文