Search code examples
nhibernatenhibernate-mapping-by-code

How does the ConventionModelMapper determine set/bag relationships


I'm using mapping by code in a new application and seeing some odd behavior with the convention model mapper. It's selecting all but one of my 1..N relationships as a Bag and one as a Set. As far as I can see, they're all declared basically the same way. More-over if I dont set up an event handler for BeforeMapSet, the appropriate BeforeMapManyToOne handler is never fired.


Solution

  • The XML mapping is more explicit in this way and usually maps IList<> to <bag> and ISet to <set>. There is an explanation, how the Fluent engine (from version 1.2) tries to do the same:

    https://github.com/jagregory/fluent-nhibernate/wiki/Fluent-mapping

    (see section HasMany / one-to-many / Collection Types)

    An extract from the link above:

    public IList<Child> Children { get; set; }    
    HasMany(x => x.Children); // <bag />
    
    
    private ISet<Child> _children;    
    public IEnumerable<Child> Children
    {
      get { return _children; }
    }
    
    HasMany(x => x.Children); // <set access="nosetter.camelcase-underscore" />
    
    
    private ISet<Child> _children;    
    public IEnumerable<Child> GetChildren()
    {
      return _children;
    }
    
    HasMany(x => x.GetChildren()); // <set name="_children" access="field" />
    

    So I would guess, that your "small differencies" in mapping could be hidden there