c# - 不存在从 DbType UInt64 到已知 SqlDbType 的映射。 Dapper C#

标签 c# sql-server dapper

我在我们的 SQL Server 2016 数据库中有一个表字段( TotalPrice ) numeric(20, 3)数据类型。
我用过 ulong type 作为我的 C# 属性类型。

public ulong TotalPrice { get; set; }
当我想在我的表中插入一条记录时,发生了以下异常。

No mapping exists from DbType UInt64 to a known SqlDbType


我的 C# 代码插入记录:
const string queryInsertInvoice = @"
INSERT INTO Invoice (CustomerId, Number, TotalPrice, LatestStatusDateTime) 
VALUES (@CustomerId, @Number, @TotalPrice, @LatestStatusDateTime)
SELECT SCOPE_IDENTITY();";

var invoiceId = await dbConnection.QueryFirstOrDefaultAsync<int>(queryInsertInvoice, invoice);
我如何使用 Dapper 2.0.53 处理这种情况?

最佳答案

这是故意的。 SQL Server 没有无符号整数,因此它试图阻止您将数据存储为与您期望的不同的东西 - 环绕对等式运算很好,但如果我们允许的话,不等式运算对于某些值的行为会非常出乎意料。
换句话说:使用 long ,不是 ulong .

编辑:我可以在“Dapper 应该允许 ulong 用于前 63 位 - 将其强制为 long - 如果设置 MSB 并抛出和异常( OverflowException ?)”上出售。这只是今天没有实现。

关于c# - 不存在从 DbType UInt64 到已知 SqlDbType 的映射。 Dapper C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63525978/

相关文章:

c# - 方法的可枚举和延迟执行

c# - 如何从gridview中获取列名?

c# - 从文件系统获取图像并使用 HttpClient 将它们发送到 C# 中的 API 的好方法

c# - 从 OpenFileDialog 处理多个选定的文件

mysql - 使用 MAX() 和 GROUP BY 的关联子查询

.net - Dapper:找不到方法: 'System.Collections.Generic.IEnumerable` 1&lt;!!0> GridReader.Read(Boolean)'

dapper - 返回一个结果集时,Dapper-dot-Net 的 "Query"和 "QueryMultiple"在性能和行为上是否相同?

c# - 更新语句以跟踪数据库中的 SortOrder

c# - 参数化动态sql查询

c# - Web Api IEnumerable 顺序