0

我正在尝试使用 Dapper 查询一个如下所示的 Sqlite 表:

CREATE TABLE "Running" (
    "Id"    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    "MachineName"   BLOB NOT NULL DEFAULT 'machine' UNIQUE,
    "IsOnline"  INTEGER DEFAULT 0,
    "TimesRan"  INTEGER DEFAULT 0,
    "LoginDateTime" TEXT,
    "LogoutDateTime"    TEXT,
    "ForceUpgrade"  INTEGER NOT NULL DEFAULT 0,
    "Version"   TEXT
)

目前在该表中,我只有一条记录:

1 DESKTOP-KQT7CGC 0
1
2019-09-21 03:04:10.099067
2019-09-21 03:04:37.6825109 0
4.0.0

var data = await _globalConfig.Connection.GetStatusAsync();

    public async Task<List<ApplicationInfoModel>> GetStatusAsync()
    {
        using (IDbConnection cnn = new SQLiteConnection(ConfigurationManager.GetLocalOrSharedConnectionString(), true))
        {
            var output = await cnn.QueryAsync<ApplicationInfoModel>("select * from Running", new DynamicParameters());

            return output.ToList();
        }
    }

ApplicationInfoModel看起来像这样:

public class ApplicationInfoModel
{
    public int Id { get; set; }

    public string MachineName { get; set; }

    public bool IsOnline { get; set; }

    public int TimesRan { get; set; }

    public DateTime LoginDateTime { get; set; } = DateTime.Now;

    public DateTime LogoutDateTime { get; set; }

    public bool ForceUpgrade { get; set; }

    public string Version { get; set; }

}

我收到一条System.Data.DataException消息Error parsing column 1 (MachineName=System.Byte[] - Object)

这是调用堆栈:

   at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in C:\projects\dapper\Dapper\SqlMapper.cs:line 3633
   at Dapper.SqlMapper.<QueryAsync>d__33`1.MoveNext() in C:\projects\dapper\Dapper\SqlMapper.Async.cs:line 439
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SdeHelper.Library.Main.DataAccess.SQLiteConnector.<GetStatusAsync>d__11.MoveNext() in G:\Users\mdumi\GitRepos\sdehelper\SdeHelper.Library.Main\DataAccess\SQLiteConnector.cs:line 154
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at SdeHelper.WpfUI.Framework.Helpers.ApplicationManager.<StartApplication>d__3.MoveNext() in G:\Users\mdumi\GitRepos\sdehelper\SdeHelper.WpfUI.Framework\Helpers\ApplicationManager.cs:line 39

内部异常:

Message: Object must implement IConvertible.

   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType)

有人可以帮我理解为什么要IConvertible在模型上实现吗?

我试图IConvertible只为MachineName属性实现,但它仍然抛出相同的异常。

4

1 回答 1

2

在这种特定情况下Object 必须实现 IConvertible内部异常意味着 Dapper 无法自动将您的MachineNameDB 字段从 DB 类型转换BLOB为模型类型string

TEXT您应该对列使用 DB 类型MachineName(这应该是正确的选择),或者在 SQLselectTEXT正确地转换它。像这样:

select cast(MachineName AS TEXT), IsOnline, TimesRan, LoginDateTime, LogoutDateTime, ForceUpgrade, Version from Running

这两个选项都应该解决您当前的异常问题。

有关转换为 TEXT 的更多信息,请参见此处:Sqlite:如何为 BLOB 转换(数据为 TEXT)

于 2019-09-21T08:45:06.120 回答