Search code examples
c#nhibernatefluent-nhibernateautomapping

Fluent nHibernate with Automapping: How to set Inverse on "Child" side of relation


According to this topic Fluent nHibernate with Automapping: How to get a parent or "containg" object from a child

I've made class with "parent" property:

class Box
{
    public virtual int Id { get; protected set;}
    public virtual IList<Item> Items {get; protected set;}
}

class Item
{
    public virtual int Id { get; protected set;}
    public virtual Box Parent {get; set;}
}

As suggested in above topic, I wrote the convention that sets keycolumn of Parent property to same as keycolumn of Items in Box. The difference is that I would like to set Inverse on Parent property, not on collection side. Is there any possibility to do that?
I've tried to write proper implementation of IReferenceConvention, but I don't see that possibility there.


Solution

  • there is no direct syntax to make the many-to-one relation the inverse one.

    There are some reasons for this:

    • it is more natural
    • and performs better, because the reference in the class (Parent) directly maps to the foreign key in the database
    • since it needs to be consistent in memory anyway (this is the part you have to care about yourself), it doesn't matter.

    You can trick it by making none inverse and suppress updates of the Parent property. I don't know if it is possible with Fluent. I wouldn't do it anyway. It's just asking for troubles.

    With XML mapping, it looks like this:

    <many-to-one name ="Parent"  ... insert="false" update="false">
    

    Example here: http://www.codeproject.com/Articles/472019/Object-Relational-Mapping-ORM-u