枚举解析不';我似乎无法与流利的NHibernate合作
我有一个数据访问类,它有一个名为sallation的枚举:枚举解析不';我似乎无法与流利的NHibernate合作,nhibernate,fluent-nhibernate,enums,nhibernate-mapping,Nhibernate,Fluent Nhibernate,Enums,Nhibernate Mapping,我有一个数据访问类,它有一个名为sallation的枚举: public enum Salutation { Unknown = 0, Dame = 1, etc Mr = 5, etc } 我正在用NHibernate学习这门课,直到今天早上我还在使用.hbm.xml文件进行映射。但是,我现在已经切换到使用Fluent NHibernate,但是加载该类的实例失败(例如): [HibernateException:无法将5解析为问候语] 如您
public enum Salutation
{
Unknown = 0,
Dame = 1,
etc
Mr = 5,
etc
}
我正在用NHibernate学习这门课,直到今天早上我还在使用.hbm.xml文件进行映射。但是,我现在已经切换到使用Fluent NHibernate,但是加载该类的实例失败(例如):
[HibernateException:无法将5解析为问候语]
如您所见,5应该可以解析为称呼语(假设5是int,则无法从错误消息中判断)
有人知道这是怎么回事吗
谢谢
大卫这比我想象的要容易得多
Map(x => x.WhateverThePropertyNameIs).CustomType(typeof(MyEnumeration));
另一个选项是使用,枚举约定:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestApp
{
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
public class EnumConvention :
IPropertyConvention,
IPropertyConventionAcceptance
{
#region IPropertyConvention Members
public void Apply(IPropertyInstance instance)
{
instance.CustomType(instance.Property.PropertyType);
}
#endregion
#region IPropertyConventionAcceptance Members
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum ||
(x.Property.PropertyType.IsGenericType &&
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
);
}
#endregion
}
}
...
var fluentCfg = Fluently.Configure().Database(cfg).Mappings(
m => m.FluentMappings.AddFromAssemblyOf<SomeObjectMap>().Conventions.Add<EnumConvention>());
...
否则,NHibernate将假定数据库中的枚举是由其字符串值表示的,而不是由其int值表示的。啊,好的。在我看来,这似乎是一个奇怪的违约选择。
Map(x => x.SomeEnumField);