(Fluent) NHibernate: force foreign key to null on delete – Education Career Blog

I have a standard parent – child (1:many) relationalship, configured using Fluent NHibernate:

On the parent side:

HasMany(x => x.Items).Inverse().Cascade.All();

and on the child side:

Map(x => x.ItemCategory).Nullable().Index("idx_item_category").Not.LazyLoad()

(Edit in response to epitka’s comment:)

The record is deleted by calling


This is the only operation done in the transaction.

(End Edit)

Currently when I delete an ItemCategory record it cascade the delete to all the items, which appears to be working as expected according to the documentation.

What I want is for Item.ItemCategory to be set to null automatically when the ItemCategory record is deleted.

I can only seem to turn off the cascade completely, which leads to a broken database (item’s referencing a missing category). So, currently I have to do this manually which is a little more error prone than I’d like.

Is it possible to configure this behaviour?


Whil it’s not possible to do that out of the box, you can probably implement an IPreDeleteEventListener that fires an HQL update to set the Items’ ItemCategory to null.

Leave a Comment