Fluent NHibernate 映射可空枚举

2024-04-06

我需要在我的类中映射一个可为空的枚举,但出现异常。

NHibernate.PropertyAccessException:无效的转换(检查您的映射是否属性类型不匹配); App.Model.Stock 的 setter ---> System.InvalidCastException: 指定的转换无效。

我已将问题范围缩小到一个特定的属性,我将在下面对此进行描述。

这个问题之前已经回答过了here https://stackoverflow.com/questions/836062/how-can-i-map-a-nullable-enum-in-nhibernate,但解决方案链接到不再存在的页面。

这是我的代码,我已将其简化为仅包含我关心的部分。

public enum eColor
{
    Red,
    Blue
}

public class Stock 
{
    public virtual eColor? Color { get; protected set; }
}

这是我的映射(精简):

public class StockMap : ClassMap<Stock>
{
    Map(x => x.Color).CustomType<int>();
}

我已尝试以下所有方法,但结果相同:

Map(x => x.Color).CustomType<int>();
Map(x => x.Color).CustomType<int?>();
Map(x => x.Color).CustomType<int>().Nullable();
Map(x => x.Color).CustomType<int?>().Nullable();

这似乎是很久以前的一个错误,并且有一个解决方法。我正在使用 Fluent 1.3.0.0 和 NHibernate 3.3.1.4000。


您应该在 CustomType() 中指定枚举类型,例如自定义类型()。这将为您提供数据库中的整数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Fluent NHibernate 映射可空枚举 的相关文章

随机推荐