gpt4 book ai didi

c# - OrmLite for ServiceStack 3 不支持可空枚举属性?

转载 作者:太空宇宙 更新时间:2023-11-03 13:20:58 28 4
gpt4 key购买 nike

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

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

在我的 Oracle 数据库中,字段 RESULT 是一个 NULLABLE NUMBER

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

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

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

最佳答案

您在 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);
}
}

关于c# - OrmLite for ServiceStack 3 不支持可空枚举属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24204639/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com