2

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

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

在我的 Oracle DB 中,该字段RESULTNULLABLE NUMBER.

当我尝试这样检索时CallSession

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

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

4

1 回答 1

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);
        }
    }
于 2015-02-18T10:01:08.560 回答