OrmLite SqlList<T> 不适用于可空枚举 属性?

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

OrmLite SqlList 不适用于 nullable enum 属性?

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

如果我有这样的枚举

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

我有一个这样的对象

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

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

当我做的时候

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

如果我只有不可为空的枚举,查询工作正常,如果我只有可为空的枚举,查询将抛出异常

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>

我正在使用单声道,但我怀疑这会是原因。数据库是 mysql。听起来 "GetColumnDbType ".

不支持可空枚举

如有任何建议,我们将不胜感激。

您使用的是最新版本吗?您有完整的示例吗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));