c# – 为什么dapper将每个数字列作为Int64返回?

前端之家收集整理的这篇文章主要介绍了c# – 为什么dapper将每个数字列作为Int64返回?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下查询
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]

其中用于查询sqlite内存数据库

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.

猜你在找的C#相关文章