Direction of Many-to-many mapping

Original Post Marta's Avatar

Marta

30 Apr, 2010 01:57 PM via web

Hi,
How are you?
I have recently discovered Fluent NHibernate, and there is something that I don't undestand about the way Automapping resolve many-to-many relationships.
Initially I thougth the mapping would be bidirectional, but then I discover that is unidirectional and I don't know how to specify the
direction.
Is there any way to do it?

Thank you very much,
Marta.

  1. 2 Posted by Devon Lazarus on 05 May, 2010 02:46 PM

    Devon Lazarus's Avatar

    The mapping is only bi-directional if you specify the collection in another entity.

    "Setting the direction" is kind of a misnomer in my opinion. You're not really setting the direction, you're telling NH which side of the collection is responsible for cascading the save/update/delete call.

    If you want to control this, you would use .Inverse() on the side of the collection you do not want to cascade from:

    public class EntityOne {
        ...
        HasManyToMany<MyEntity>(x => x.Entities)
            .AsBag()
            .Inverse();
    }
    
    public class EntityTwo {
        ...
        HasManyToMany<MyEntity>(x => x.Entities)
            .AsBag();
    }
    

    Using this mapping, MyEntity objects added to EntityOne would not be saved in the database unless they were also added to the EntityTwo.Entities collection.

    hth,

    -devon

  2. James Gregory resolved this discussion on 16 May, 2010 05:24 PM.

Comments are currently closed for this discussion. You can start a new one.

Recent Discussions

05 Jul, 2010 10:29 PM
05 Jul, 2010 12:45 PM
05 Jul, 2010 12:42 PM
05 Jul, 2010 12:17 PM
05 Jul, 2010 12:12 PM

 

03 Jul, 2010 12:26 AM
02 Jul, 2010 02:17 PM
02 Jul, 2010 08:18 AM
02 Jul, 2010 12:20 AM
01 Jul, 2010 10:14 PM