Ivan Koshelev
Ivan Koshelev

Reputation: 4280

Nullable enum properties not supported in OrmLite for ServiceStack 3?

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

Answers (1)

Aleksey Mynkov
Aleksey Mynkov

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

Related Questions