Fluent NHibernate mapping nullable enum

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:33:06

问题


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.


回答1:


You should specify the enum type in CustomType<T>(), e.g. CustomType<eColor>(). This will give you integers in the database.



来源:https://stackoverflow.com/questions/13881971/fluent-nhibernate-mapping-nullable-enum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!