Search code examples
c#nhibernatefluent-nhibernateusertypeiusertype

mapping a collection via IUserType


I need to load a class (Adjustment) with some logic, so I implemented the IUserType in Nhibernate. When it's a single reference, it works fine:

    Map(p => p.DefaultAdjustment)
        .CustomType(typeof(AdjustmentCustomMap));

Its SqlType is string.
However, I have a collection from Adjustment as well, I dont know how can I map it !

        HasMany(p => p.Adjustments)
            .Cascade.AllDeleteOrphan()
            .Inverse())
            ;

how can I get my implementation of IUserType to load the collection?
I'm using NH 3.3
Thanks in advance.


Solution

  • Try using .Element() with your HasMany mapping. Since you are not mapping a collection of entities, but a collection of simple objects, you need to use the same mapping as you would have if mapping a collection of string, int, Guid...

    HasMany(p => p.Adjustments)
        .Element("Adjustment", e => e.Type<AdjustmentCustomMap>())
        .Cascade.AllDeleteOrphan()
        .Inverse())
        ;
    

    The first parameter of Element method is a column name.