NHibernate 删除实体 - 检查引用
当用户删除实体但由于存在对其的引用而无法删除该实体时,是否有通用的解决方案来检查引用? 我寻找一个适用于所有情况的解决方案。用户尝试删除,我可以签入我的数据访问层并给用户一条消息“无法删除实体,因为存在引用”。你如何解决这个问题?
Is there a generic solution to check for references when the user will delete an entity and it could not be deleted because there are references to it?
I search a solution which works in all situation. The user try to delete, I can check in my DataAccess-Layer and give the user a message "Entity could not be deleted because there are references". How do you solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不需要知道引用是什么,您可以在 DAL 中捕获 FK 异常,并将其包装到 Service 层中的 ReferencedObjectsException 中,并带有适当的错误消息。
否则,您将必须查看映射(通过代码)、确定引用(通过代码)并检查依赖关系(通过代码)。
Rhino.Security 在这里有一个映射重写部分: https://github.com/ayende/rhino-security/blob/master/Rhino.Security/Impl/MappingRewriting/UserMapper.cs,来自NHibernate 配置对象,您可以访问类映射。
It you don't need to know what references are, you can catch the FK exception in you DAL, and wrap it to your Service layer into a ReferencedObjectsException, with appropriate error message.
Otherwise, you would have to look at the mappings (by code), determine the references (by code), and check for dependencies (by code).
Rhino.Security has a Mapping rewriting part here : https://github.com/ayende/rhino-security/blob/master/Rhino.Security/Impl/MappingRewriting/UserMapper.cs, from the NHibernate configuration object, you have access to the Class Mappings.
您需要映射相关实体并包含
cascade='all-delete-orphan'
选项。You need to map the related entities and include the
cascade='all-delete-orphan'
option.