使用 Fluent NHibernate 和 NHibernate 3 将枚举映射为 Int [英] Map Enum as Int with Fluent NHibernate and NHibernate 3

查看:28
本文介绍了使用 Fluent NHibernate 和 NHibernate 3 将枚举映射为 Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这如何与 NHibernate 3 配合使用?

How does this work with NHibernate 3?

更新

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

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>

当为枚举指定了 IUserTypeConvention 时,使用 GenericEnumMapper 似乎是不对的.

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

这是我的约定:

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 );
    }
}

推荐答案

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

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

如果有人知道为什么 IUserTypeConvention 在 NHibernate 3 中不能与 Fluent NHibernate 一起工作,我仍然想知道为什么.也许是因为将自定义类型映射到枚举现在可以工作,但是为什么当时没有从库中删除它?

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?

这篇关于使用 Fluent NHibernate 和 NHibernate 3 将枚举映射为 Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆