【问题标题】:OrmLite SqlList<T> doesn't work with nullable enum property?OrmLite SqlList<T> 不适用于可为空的枚举属性?
【发布时间】:2015-07-23 04:01:30
【问题描述】:

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 异常 System.Collections.Generic.KeyNotFoundException:给定的键不在字典中。 在 System.ThrowHelper.ThrowKeyNotFoundException () [0x00000] 在 /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 在 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)

我正在使用单声道,但我怀疑这会是原因。数据库是mysql。听起来“GetColumnDbType”不支持可为空的枚举。

任何建议将不胜感激。

【问题讨论】:

    标签: c# mysql enums servicestack ormlite-servicestack


    【解决方案1】:

    你使用的是最新版本吗,有没有完整的例子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));
    

    【讨论】:

    • 更正:我使用的是 4.0.36。我会检查最新版本,看看问题是否仍然存在。
    • 是的,4.0.42 没有问题。 4.0.36 不支持此功能(可为空)吗?
    • @Jeff v4.0.36 已于 6 个多月前发布,从那时起问题可能已得到解决。尽管具有可为空的枚举并不常见,但通常您会使用枚举值之一来表示默认/空值。
    • 我们使用 nullable 的原因是因为它可以让我们像下面的 sql 一样进行“过滤”(即 where 子句)。 "select w.Blah,w.Blah1,w.Blah2,w.Status from works w where(@Status is null or w.Status = @Status)"
    • 并不是说没有更好的方法,我只是在描述已经做了什么。再次感谢您的及时回复! ServiceStack 太棒了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    相关资源
    最近更新 更多