【问题标题】:Nullable enum properties not supported in OrmLite for ServiceStack 3?OrmLite for ServiceStack 3 不支持可空枚举属性?
【发布时间】:2014-06-13 11:50:05
【问题描述】:

我正在使用 ServiceStack 3 和 OrmLite。 我的一个数据类有一个可以为空的枚举属性,如下所示:

[Alias("CALL_SESSION")]
public class CallSession
{
    ...
    [Alias("RESULT")]
    public CallSessionResultEnum? Result { get; set; }
    ...
} 

在我的 Oracle DB 中,RESULT 字段是 NULLABLE NUMBER

当我尝试像这样检索CallSession 时:

cn.Where<CallSession>(x => ....)

我得到一个异常specified cast is not valid。 如果我将类中的字段类型切换为简单的int?,它工作正常。我认为 OrmLite 不支持可为空的枚举是否正确?

【问题讨论】:

    标签: c# servicestack ormlite-servicestack servicestack-bsd


    【解决方案1】:

    您在 OracleOrmLiteDialectProvider 中错误地覆盖了 ConvertDbValue 方法:

        public override object ConvertDbValue(object value, Type type)
        {
            if (type.IsEnum)
            {
                var val = Convert.ToInt32(value);
                if (!Enum.IsDefined(type, val))
                    throw ExHelper.Argument(Errors.Common_EnumValueNotFound, val, type.FullName);
    
                return val;
            }
        }
    

    解决方案:

        public override object ConvertDbValue(object value, Type type)
        {
            if (type.IsEnum)
            {
                var val = Convert.ToInt32(value);
                if (!Enum.IsDefined(type, val))
                    throw ExHelper.Argument(Errors.Common_EnumValueNotFound, val, type.FullName);
    
                return base.ConvertDbValue(value, type);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多