Map Enum as Int with Fluent NHibernate and NHibernate 3

前端 未结 3 651
小鲜肉
小鲜肉 2020-12-29 05:00

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 anym

相关标签:
3条回答
  • 2020-12-29 05:20

    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?

    0 讨论(0)
  • 2020-12-29 05:31

    You should inherit your convention not from IUserTypeConvention, but from FluentNHibernate.Conventions.UserTypeConvention.

    For example, this is the exact convention I use to map boolean and nullable booleans to a custom type called UserTrueFalseType:

        /// <summary>
    /// Convention: Boolean fields map to CHAR(1) T/F/Null
    /// </summary>
    public class BooleanTrueFalseConvention : FluentNHibernate.Conventions.UserTypeConvention<UserTrueFalseType>
    {
        /// <summary>
        /// Accept field type criteria
        /// </summary>
        /// <param name="criteria"></param>
        public override void Accept(FluentNHibernate.Conventions.AcceptanceCriteria.IAcceptanceCriteria<FluentNHibernate.Conventions.Inspections.IPropertyInspector> criteria)
        {
            criteria.Expect(instance =>
                instance.Property.PropertyType.Equals(typeof(System.Boolean))
                ||
                instance.Property.PropertyType.Equals(typeof(System.Nullable<System.Boolean>))
            );
        }
    }
    

    This works with NH 3.3 and the last version of Fluent.

    0 讨论(0)
  • 2020-12-29 05:45

    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

    0 讨论(0)
提交回复
热议问题