Search code examples
nhibernatecascadefluentone-to-one

Fluent NHibernate one-to-one doesn't have cascade all-delete-orphan


I am in the process of updating the existing *.hbm.xml files into fluent ClassMaps and have stumbled on a mistake in our mapping files, and I don't know the default behavior for me to map this correctly.

The mapping file has:

<one-to-one name="LineItemAssembly" 
            class="LineItemAssembly" 
            cascade="all-delete-orphan" />

When using Fluent, I would expect this to map to:

HasOne<LineItemAssembly>(x => x.LineItemAssembly)
                        .Cascade.AllDeleteOrphan();

However, AllDeleteOrphan() is not an option off of Cascade (and correctly so). I understand that is because it isn't an option and is because it is an error in the mapping file. What would be the equivalent mapping using Fluent so that my fluent mapping has the exact same functionality as the .hbm.xml file? I would think just leaving Cascade out of the Fluent mapping would be the default behavior, but I just don't know for sure.


Solution

  • In the end, I just needed to figure out what was wrong and fix this, regardless of the default fluent behavior. With the help of http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/ I was able to solve this issue.

    I needed to add .Cascade.All(). However, what I really needed was a .ForeignKey() off the HasOne relationship in my mapping for the LineItemAssembly class.