C# 能否将NHibernate配置为自动调用。CustomType<;T>;对于某一类型的所有列?
我有一个具有枚举类型属性的模型:C# 能否将NHibernate配置为自动调用。CustomType<;T>;对于某一类型的所有列?,c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,我有一个具有枚举类型属性的模型: public virtual Units Unit { get; set; } // Units is an enum 我有一个名为EnumMapper的类,它处理一些用于数据库的自定义映射逻辑。在我的映射中,我有: Map(x => x.Unit).CustomType<EnumMapper<Units>>(); 我有一种感觉,这就像调用.Conventions.Add()其他东西一样简单,但我似乎无法正确地执行它。使用Con
public virtual Units Unit { get; set; } // Units is an enum
我有一个名为EnumMapper的类,它处理一些用于数据库的自定义映射逻辑。在我的映射中,我有:
Map(x => x.Unit).CustomType<EnumMapper<Units>>();
我有一种感觉,这就像调用
.Conventions.Add()
其他东西一样简单,但我似乎无法正确地执行它。使用ConventionBuilder
找到了一个解决方案:
.Conventions.Add(ConventionBuilder.Property.When(
c => c.Expect(x => x.Type == typeof(GenericEnumMapper<Units>)),
x => x.CustomType<EnumMapper<Units>>()
))
.Conventions.Add(ConventionBuilder.Property.When(
c=>c.Expect(x=>x.Type==typeof(genericenumapper)),
x=>x.CustomType()
))
它不是超级漂亮,但它很管用。我认为更好的解决方案可能是编写自己的自动将枚举映射到其正确的自定义类型。我会在这里张贴,如果我得到它的工作
更新:稍微清理了一下。我已向我的EnumMapper
类添加了一个静态方法:
public class EnumMapper<T> : NHibernate.Type.EnumStringType<T>
{
// Regular mapping code here
public static IPropertyConvention Convention
{
get
{
return ConventionBuilder.Property.When(
c => c.Expect(x => x.Type == typeof (GenericEnumMapper<T>)),
x => x.CustomType<EnumMapper<T>>()
);
}
}
}
公共类EnumMapper:NHibernate.Type.EnumStringType
{
//这里是正则映射代码
公共静态知识产权公约
{
得到
{
返回ConventionBuilder.Property.When(
c=>c.Expect(x=>x.Type==typeof(genericenumapper)),
x=>x.CustomType()
);
}
}
}
现在,我可以使用以下配置:
.Conventions.Add(EnumMapper<Units>.Convention)
.Conventions.Add(EnumMapper.Convention)
.Conventions.Add(EnumMapper<Units>.Convention)