OrmLite SqlList <t>不能与可空的枚举属性一起使用?

时间:2015-07-23 04:01:31

标签: c# mysql enums servicestack ormlite-servicestack

OrmLite SqlList 不适用于可以为空的枚举属性?

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:给定的键不在字典中。   在/private/tmp/source-mono-mac-4.0.0-branch-c5sr2/bockbuild-mono-4.0.0-branch/profiles/mono-mac-xamarin/build中的System.ThrowHelper.ThrowKeyNotFoundException()[0x00000] -root /单4.0.2 /外部/ referencesource / mscorlib程序/系统/ throwhelper.cs:70   在System.Collections.Generic.Dictionary 2<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.OrmLiteDialectProviderBase 1.GetColumnDbType(System.Type)&lt; 0x00093&gt;

我是单音,但我怀疑这是原因。数据库是mysql。 “GetColumnDbType”不支持“nullable enum”这样的声音。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:2)

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