流畅的NHibernate映射可以为空的枚举 [英] Fluent NHibernate mapping nullable enum
问题描述
NHibernate.PropertyAccessException:无效的Cast(检查您的映射关于属性类型不匹配); Set.Model.Stock ---> System.InvalidCastException:指定的转换无效
我将问题缩小到一个特定的属性,我描述
以前已回答这里,但解决方案链接到不再存在的页面。
这是我的代码,我已经减少到只包含我关心的部分。
public enum eColor
{
Red,
蓝色
}
public class Stock
{
public virtual eColor?颜色{get;保护套
}
这是我的映射(剥离):
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< T> ),例如CustomType<易彩>()。这将给数据库中的整数。
I need to map a nullable enum in my class but am getting exceptions.
NHibernate.PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of App.Model.Stock ---> System.InvalidCastException: Specified cast is not valid.
I have narrowed the issue down to one specific property, which I describe below.
This was previously answered here, but the solution links to a page which no longer exists.
Here is my code, which I have reduced to contain only the portions I am concerned with.
public enum eColor
{
Red,
Blue
}
public class Stock
{
public virtual eColor? Color { get; protected set; }
}
Here is my mapping (stripped down):
public class StockMap : ClassMap<Stock>
{
Map(x => x.Color).CustomType<int>();
}
I have tried all of the following with the same results:
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();
This appeared to be a bug a long time ago and there was a workaround. I am using Fluent 1.3.0.0 and NHibernate 3.3.1.4000.
You should specify the enum type in CustomType<T>(), e.g. CustomType<eColor>(). This will give you integers in the database.
这篇关于流畅的NHibernate映射可以为空的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!