开发者

NHibernate on-delete="cascade" with relationship on many side

开发者 https://www.devze.com 2023-04-12 19:50 出处:网络
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.

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" opti开发者_开发问答on 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号