Reputation: 4280
I'm using ServiceStack 3 and OrmLite. One of my data classes has a nullable enum property like this:
[Alias("CALL_SESSION")]
public class CallSession
{
...
[Alias("RESULT")]
public CallSessionResultEnum? Result { get; set; }
...
}
In my Oracle DB the field RESULT
is a NULLABLE NUMBER
.
When I try to retrieve CallSession
like this:
cn.Where<CallSession>(x => ....)
I get an exception specified cast is not valid
.
It works fine if I switch the field type in my class to a simple int?
. Am I correct in thinking that OrmLite does not support nullable enums?
Upvotes: 2
Views: 253
Reputation: 101
You have incorrect override of method ConvertDbValue in your OracleOrmLiteDialectProvider:
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;
}
}
Solution:
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);
}
}
Upvotes: 1