我正在嘗試使用 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)目前在該表中我只有一條記錄:1DESKTOP-KQT7CGC 012019-09-21 03:04:10.0990672019-09-21 03:04:37.6825109 04.0.0var 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)。有人可以幫助我理解為什么我應(yīng)該在模型上實(shí)現(xiàn)IConvertible嗎?我嘗試僅針對(duì) 屬性實(shí)現(xiàn) IConvertible,但它仍然引發(fā)相同的異常。MachineName
1 回答

慕仙森
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
在這種特定情況下對(duì)象必須實(shí)現(xiàn) IConvertible?內(nèi)部異常意味著 Dapper 無法自動(dòng)轉(zhuǎn)換您的MachineName
?DB字段從數(shù)據(jù)庫類型?BLOB
?到模型類型?string
您應(yīng)該對(duì)?列使用數(shù)據(jù)庫類型?TEXT
(這應(yīng)該是正確的選擇),或者將其轉(zhuǎn)換為 SQL?正確地。像這樣:MachineName
select
TEXT
select?cast(MachineName?AS?TEXT),?IsOnline,?TimesRan,?LoginDateTime,?LogoutDateTime,?ForceUpgrade,?Version?from?Running
這兩個(gè)選項(xiàng)都應(yīng)該可以解決您當(dāng)前的問題(例外情況)。
- 1 回答
- 0 關(guān)注
- 255 瀏覽
添加回答
舉報(bào)
0/150
提交
取消