使用 NHibernate 限制删除
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想不出在 NHibernate 中执行此操作的方法,因为这需要 NHibernate 对这些关系有一定的了解。我将使用 规范模式 在代码中处理此问题。例如(使用带有指向 Employee 对象的链接的 Company 对象):
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):