Sleeping cascade removes on the bag

Having created most of my DAL with NHibernate, I now find that the SQL Server Cascade On Delete rules in my HBM files also need to be considered. I use packages for all my collections, but there doesn't seem to be a way to add the cascade = "delete" attribute to the bag. I can change all my bag sets, but that means I change all of my IList <> s to my Models to PersistentGenericSet <> s which I really don't like.

Any advice?

Anthony

+2


a source to share


2 answers


There are two concepts that are related to each other: NHibernate cascading rules and database cascading.

The latter is actually tuned to the element key

, so if FK has ON DELETE CASCADE

, this is what it should look like in the bag:



<bag name="Children" ...>
  <key column="ParentId" on-delete="cascade"/>
  <one-to-many class="Child"/>
</bag>

      

+4


a source


You can add the cascade attribute to the package mapping. The documentation lists several attribute parameters cascade="all|none|save-update|delete|all-delete-orphan"

. The most commonly used option for a one-to-many relationship is all-delete-orphan

. Setting the cascade to this value will cascade all database operations in the collection, and the child objects will be deleted if removed from the collection (lost).



Database cascades are similar, but do not provide the ability to automatically delete an orphaned child record. Setting the cascade parameter in NHibernate and in the database is somewhat redundant, but can be useful if you have other systems accessing the database directly.

+2


a source







All Articles