使用Fluent NHibernate和NHibernate 3将Enum作为Int映射

Jos*_*ose 23 nhibernate nhibernate-mapping fluent-nhibernate

我用过这个如何使用流畅的NHibernate将枚举映射为int值?在过去映射,但我最近升级到NHibernate 3,这似乎不再起作用了.我在我的EnumConvention课程中放了断点,但它们没有被击中.命中数据库的查询将枚举作为字符串,这是默认配置.

这如何与NHibernate 3一起使用?

更新

以下是生成的映射文件的一部分:

<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>
Run Code Online (Sandbox Code Playgroud)

如果为枚举指定了GenericEnumMapper何时使用它似乎是正确的IUserTypeConvention.

这是我的惯例:

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 );
    }
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*ose 45

简单地做Map( m => m.MyEnum ).CustomType<MyEnum>()似乎现在工作得很好.

如果有人知道为什么IUserTypeConventionNHibernate 3中不能使用Fluent NHibernate,我仍然想知道原因.也许这是因为将自定义类型映射到枚举现在可以正常工作,但为什么它不会从lib中删除呢?