我有以下查询:
SELECT [Person].[Id] AS [Person_Id],[Person].[Name] AS [Person_Name],[Address].[Id] AS [Address_Id],[Address].[Number] AS [Address_Number],[Address].[Street] AS [Address_Street],FROM [Person] LEFT JOIN [Address] ON [Person].[addressId] = [Address].[Id]
var rows = _database.Query(query);
但是,当我尝试将值读取为Int32时,我得到InvalidCastException.
foreach (IDictionary<string,object> row in rows) { var someId = (int)row["Person_Id"]; var someNumber = (int)row["Address_Number"]; }
我之所以如此使用dapper的原因在于,作为一个侧面项目,我在dapper上构建一些功能,将值映射到POCO(我知道类似的项目,例如rainbow,dapper.extensions等).
所以有些东西:
var rowVal = row[fieldName]; propInfo.SetValue(obj,rowVal,null);
在上面的示例中,POCO的属性类型是int32,由于此问题,我无法将值分配给obj.
任何帮助或想法都非常感谢.
解决方法
它不是Dapper,而是sqlite.请参阅
Datatypes In SQLite Version 3(假设您实际使用的是v3):
The INTEGER storage class,for example,includes 6 different integer datatypes of different lengths. This makes a difference on disk. But as soon as INTEGER values are read off of disk and into memory for processing,they are converted to the most general datatype (8-byte signed integer). And so for the most part,“storage class” is indistinguishable from “datatype” and the two terms can be used interchangeably.