Jeff
Jeff

Reputation: 13199

OrmLite SqlList<T> doesn't work with nullable enum property?

OrmLite SqlList doesn't work with nullable enum property?

public static List<T> SqlList<T> (this IDbConnection dbConn, string sql, object anonType = null);

If I have an enum like so

public enum WorkStatus
{
    Started = 0,
    Ended = 1
}

And I have an object like so

public class Query
{
    //nullable enum won't work
    public WorkStatus? NotWork { get; set; }

    //but non nullable enum will work
    public WorkStatus Work { get; set; }
}

When I do

//conn is of type IDbConnection
//ignored where clause in raw sql just for the simplicity
conn.SqlList<T>(@"select * from works", new Query());

If I only have the non nullable enum the query works fine, if I only have the nullable enum, the query will throw exceptions

LEVEL:ERROR CLASS:ServiceStack.DtoUtils ServiceBase::Service Exception System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.ThrowHelper.ThrowKeyNotFoundException () [0x00000] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/throwhelper.cs:70 at System.Collections.Generic.Dictionary2<System.Type, System.Data.DbType>.get_Item (System.Type) [0x00021] in /private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.0.2/external/referencesource/mscorlib/system/collections/generic/dictionary.cs:176 at ServiceStack.OrmLite.OrmLiteDialectProviderBase1.GetColumnDbType (System.Type) <0x00093>

I'm on mono but I doubt this will be the cause. Database is mysql. It kind of sounds like nullable enum isn't supported by "GetColumnDbType ".

Any suggestions would be appreciated.

Upvotes: 0

Views: 342

Answers (1)

mythz
mythz

Reputation: 143374

Are you using the latest version and do you have a complete example as this test below works in all Databases:

var db = OpenDbConnection();

db.DropAndCreateTable<TypeWithNullableEnum>();

db.Insert(new TypeWithNullableEnum { Id = 1, 
    EnumValue = SomeEnum.Value1, NullableEnumValue = SomeEnum.Value2 });
db.Insert(new TypeWithNullableEnum { Id = 2, EnumValue = SomeEnum.Value1 });

var rows = db.Select<TypeWithNullableEnum>();
Assert.That(rows.Count, Is.EqualTo(2));

var row = rows.First(x => x.NullableEnumValue == null);
Assert.That(row.Id, Is.EqualTo(2));

var quotedTable = typeof(TypeWithNullableEnum).Name.SqlTable();

rows = db.SqlList<TypeWithNullableEnum>("SELECT * FROM {0}".Fmt(quotedTable));

row = rows.First(x => x.NullableEnumValue == null);
Assert.That(row.Id, Is.EqualTo(2));

rows = db.SqlList<TypeWithNullableEnum>("SELECT * FROM {0}"
    .Fmt(quotedTable), new { Id = 2 });

row = rows.First(x => x.NullableEnumValue == null);
Assert.That(row.Id, Is.EqualTo(2));

Upvotes: 2

Related Questions