How do you map an enum as an int value with fluent NHibernate?

后端 未结 7 1987
迷失自我
迷失自我 2020-11-27 11:04

Question says it all really, the default is for it to map as a string but I need it to map as an int.

I\'m currently using Persistenc

7条回答
  •  自闭症患者
    2020-11-27 11:33

    Don't forget about nullable enums (like ExampleEnum? ExampleProperty)! They need to be checked separately. This is how it's done with the new FNH style configuration:

    public class EnumConvention : IUserTypeConvention
    {
        public void Accept(IAcceptanceCriteria criteria)
        {
            criteria.Expect(x => x.Property.PropertyType.IsEnum ||
                (x.Property.PropertyType.IsGenericType && 
                 x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
                 x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
                );
        }
    
        public void Apply(IPropertyInstance target)
        {
            target.CustomType(target.Property.PropertyType);
        }
    }
    

提交回复
热议问题