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

问题描述

我用这个映射在过去,但我最近升级到NHibernate 3,这似乎并没有工作了。我已经在我的EnumConvention类中放置了断点,并且没有被击中。查询命中数据库的枚举为一个字符串,这是默认的配置。

这对NHibernate 3是如何工作的?



更新



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

 < property name =ComponentTypetype =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>

使用 GenericEnumMapper code>当为枚举指定 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);


$ / code $ / pre

解决方案

做$ Map(m => m.MyEnum).CustomType< MyEnum>()现在看起来工作得很好。

如果有人知道为什么在NHibernate 3中为什么 IUserTypeConvention 不能和Fluent NHibernate一起工作,我还是想知道为什么。也许这是因为将自定义类型映射到枚举现在工作,但为什么不是从lib中删除?


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 );
    }
}
解决方案

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?

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

09-05 01:52