NHibernate on-delete=“cascade” with relationship on many side

廉价感情. 提交于 2019-12-11 08:52:19

问题


I have an object model like that:

  • Folder - simple structure with name etc.
  • File - complex object containing reference to Folder in which it is contained.

Folder doesn't know its Files and I don't want it to know. The relation is many-to-one and should be known on File's side only.

Anyway, I would like to rely on my database's ON DELETE CASCADE feature, so that when I remove Folder, I want all Files within that Folder to be deleted automatically. I can't use NHibernate's cascading as there is no relation from Folder to File.

I know that there is on-delete="cascade" option for <key> element in case of one-to-many relationship, but I can't find its equivalent for my model - when the relation is defined on many side.

Am I doing something wrong or do I really need to go through and delete all the Files within deleted Folder manually?


回答1:


You could try to map the one-to-many side with access="noop". That way you don't need a property in your classes but still have the mapping.

In Fluent NHibernate that would be someting like this:

HasMany(Reveal.Member<Folder, IEnumerable<File>>("_files"))
   .KeyColumn("column_name")
   .Access.NoOp()
   .Inverse()
   .ForeignKeyCascadeOnDelete();

Note: For that you need an _files field of type IEnumerable<File> in the Folder class (limitation of Fluent NHibernate, can only map really existing fields or properties). But this field can always be null, it will never be used.



来源:https://stackoverflow.com/questions/7729892/nhibernate-on-delete-cascade-with-relationship-on-many-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!