Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map Enum as Int with Fluent NHibernate and NHibernate 3

Tags:

I used this How do you map an enum as an int value with fluent NHibernate? to map in the past but I've recently upgraded to NHibernate 3 and this doesn't seem to work anymore. I've put breakpoints in my EnumConvention class and they're not being hit. The query that is hitting the database has the enum as a string which is the default configuration.

How does this work with NHibernate 3?

Update

Here is part of the mapping file that is generated:

<property name="ComponentType" type="FluentNHibernate.Mapping.GenericEnumMapper`1[[...ComponentType, ..., Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], FluentNHibernate, Version=1.1.0.0, Culture=neutral, PublicKeyToken=8aa435e3cb308880">   <column name="ComponentTypeId" /> </property> 

It doesn't seem right that it would be using a GenericEnumMapper when an IUserTypeConvention is specified for enums.

Here is my convention:

public class EnumConvention : IUserTypeConvention {     public void Accept( IAcceptanceCriteria<IPropertyInspector> criteria )     {         criteria.Expect( e => e.Property.PropertyType.IsEnum );     }      public void Apply( IPropertyInstance instance )     {         instance.CustomType( instance.Property.PropertyType );     } } 
like image 883
Josh Close Avatar asked Mar 21 '11 19:03

Josh Close


2 Answers

Simply doing Map( m => m.MyEnum ).CustomType<MyEnum>() seems to work just fine now.

If anyone knows why IUserTypeConvention doesn't work with Fluent NHibernate in NHibernate 3, I'd still like to know why. Maybe it's because mapping the custom type to the enum works now, but why wasn't it removed from the lib then?

like image 133
Josh Close Avatar answered Sep 20 '22 12:09

Josh Close


I'm running into a similar problem with Nhibernate 3.0GA and FluentNh (rebuild with the latest NH version). UserTypeConventions are not getting registered properly.

problem described here : http://groups.google.com/group/nhusers/browse_thread/thread/c48da661f78bfad0

like image 28
vikram nayak Avatar answered Sep 20 '22 12:09

vikram nayak