3

我有一个约定whereUserTypeConvention<MyUserType>处理枚举类型。我已经这样配置了 Fluent NHibernateMyUserType : IUserTypeMyUserTypeMyEnum

sessionFactory = Fluently
                .Configure()
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(
                    c => c.Is(connectionString))
                )
                .Mappings(
                    m => m
                            .FluentMappings
                                .AddFromAssemblyOf<A>()
                            .Conventions
                                .AddFromAssemblyOf<A>()
                )
                .BuildSessionFactory();

其中A是 和 相同程序集中的UserTypeConvention<MyUserType>类型MyUserType。但是,Fluent NHibernate 不适用于我的域对象上MyUserType的类型属性。MyEnum相反,它适用FluentNHibernate.Mapping.GenericEnumMapper<MyEnumType>于这些属性。

到底是怎么回事?

4

2 回答 2

1

现在我已经解决了这个问题:

public class MyEnumUserTypeConvention : UserTypeConvention<MyEnumUserType> {
    public override void Accept(IAcceptanceCriteria<IPropertyInspector> criteria) {
        // Fluent NHibernate is too eager in applying GenericEnumMapper
        // so our criteria is that it is already applied this type
        criteria.Expect(x => x.Type == typeof(GenericEnumMapper<MyEnum>));
    }

    public override void Apply(IPropertyInstance instance) {
        // we override Fluent NHibernate's application of GenericEnumMapper
        instance.CustomType<MyEnumUserType>();
    }
}

我认为这应该是完全没有必要的。如果有人告诉我这是 Fluent NHibernate 中的一个错误,那很好。如果有人给我一个很好的理由,为什么 Fluent NHibernate 应该如此渴望应用GenericEnumMapper,那也是可以接受的。

于 2010-10-19T20:03:51.997 回答
0

好的,我尝试了以下方法,我认为它对您有用:
只需覆盖 MyEnumUserTypeConvention 类中的 Accept 方法,并且在其中什么都不做:

  public class MyEnumUserTypeConvention : UserTypeConvention<MyEnumUserType>
  {
    public override void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria<FluentNHibernate.Conventions.Inspections.IPropertyInspector> criteria)
    {
       ///Do nothing
    }
  }
于 2010-10-15T15:26:16.893 回答