Wednesday, November 16, 2011

NHibernate.MappingException: Could not determine type for: MyClass, for columns: NHibernate.Mapping.Column(id)

If you are using conformist mapping-by-code introduced by NHibernate in the 3.2, and are facing this exception maybe you have another problem.
If you define a Bag map like this
Bag(x => x.CollectionProp
     , map =>
     {
        map.Key(km => km.Column("col_id"));
        map.Cascade(Cascade.All | Cascade.DeleteOrphans);
     });
for a one-to-many association, you will retrieve this (misleading) exception. To solve it you have to specific the action in a way like this:
Bag(x => x.CollectionProp
     , map =>
     {
        map.Key(km => km.Column("col_id"));
        map.Cascade(Cascade.All | Cascade.DeleteOrphans);
     }
     , action => action.OneToMany()
    );
A post about it on stack overflow.