Search code examples
c#nhibernatefluent-nhibernatefluent-nhibernate-mapping

What do inverse and readonly attributes do in NHibernate napping do?


While I am working a project I have seen this line of NHibernate mapping

HasMany(entity => entity.Tasks).KeyColumn("APPLICATION_ID").Cascade.AllDeleteOrphan().ReadOnly().Inverse();

it is the first time for me I see some one using the inverse and readonly attributes so please could anyone explain them to me.


Solution

  • You can find a detailed description of inverse here

    TLDR; from the link

    • Inverse is a boolean attribute that can be put on the collection mappings, regardless of collection's role (i.e. within one-to-many, many-to-many etc.), and on join mapping.

    • We can't put inverse on other relation types, like many-to-one or one-to-one.

    • By default, inverse is set to false.
    • Inverse makes little sense for unidirectional relationships, it is to be used only for bidirectional ones.
    • General recommendation is to use inverse="true" on exactly one side of each bidirectional relationship.
    • When we don't set inverse, NHProf will complain about superfluous updates.

    And this link for Readonly

    TLDR;

    The only difference apart from different naming for some properties and Property vs. Map name itself is the ReadOnly method available in FNH. It is just a shortcut for setting both .Not.Insert() and .Not.Update().