简体   繁体   English

不存在从 DbType UInt64 到已知 SqlDbType 的映射。 小巧玲珑 C#

[英]No mapping exists from DbType UInt64 to a known SqlDbType. Dapper C#

I have a table field( TotalPrice ) in our SQL Server 2016 database with numeric(20, 3) data type.我的 SQL Server 2016 数据库中有一个表字段( TotalPrice ),数据类型为numeric(20, 3)

I used ulong type as my C# property type.我使用ulong类型作为我的 C# 属性类型。

public ulong TotalPrice { get; set; }

When I want to insert a record in my Table the following exception occurred.当我想在我的表中插入一条记录时,发生了以下异常。

No mapping exists from DbType UInt64 to a known SqlDbType不存在从 DbType UInt64 到已知 SqlDbType 的映射

My C# code to insert a record:我的 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);

How can I handle this situation with Dapper 2.0.53?我如何使用 Dapper 2.0.53 处理这种情况?

This is deliberate.这是故意的。 SQL Server doesn't have unsigned integers, so it is trying to prevent you from storing the data as something different to what you expected - wraparound is fine for equality operations, but inequality operations would behave very unexpectedly for some values if we allowed this. SQL 服务器没有无符号整数,因此它试图阻止您将数据存储为与您预期不同的数据 - 环绕对于相等运算很好,但如果我们允许,对于某些值,不相等运算会表现得非常意外。

In other words: use long , not ulong .换句话说:使用long ,而不是ulong


Edit: I could be sold on the "Dapper should allow ulong for the first 63 bits - coercing it to long - and throw and exception ( OverflowException ?) if the MSB is set".编辑:我可以出售“Dapper 应该允许ulong用于前 63 位 - 将其强制为long - 如果设置了 MSB,则抛出异常( OverflowException ?)”。 That just isn't implemented today.这只是今天没有实施。

When I use Dapper what I do is create separate data classes from my main model that represent the results from SQL.当我使用 Dapper 时,我所做的是从我的主要 model 创建单独的数据类,它们代表 SQL 的结果。 With these SqlModel classes, I make sure the types align exactly with how things are represented in SQL.使用这些SqlModel类,我确保类型与 SQL 中的事物表示方式完全一致。 In my C# business logic, often it is easier to use int or enums for variables where they may be TINYINT in SQL.在我的 C# 业务逻辑中,在 SQL 中它们可能是TINYINT的变量通常更容易使用int或枚举。 So I would end up with two classes like this:所以我最终会得到两个这样的类:

class UserSqlModel {
  public string Name { get; set; }
  public byte UserTypeId { get; set; }
}

class User {
  public string Name { get; set; }
  public UserType Type { get; set; }
}

Then I would write a mapping function between the two models.然后我会在两个模型之间写一个映射 function 。 This isolates your business logic from the nuances of how data is represented in SQL and allows refactoring your business logic or SQL implementation separately.这将您的业务逻辑与 SQL 中数据表示方式的细微差别隔离开来,并允许分别重构您的业务逻辑或 SQL 实现。

In your example, maybe your SqlModel would have a long property and your mapping function would handle conversion of ulong to long and would deal with overflow issues.在您的示例中,您的SqlModel可能会有一个long属性,而您的映射 function 将处理ulonglong的转换并处理溢出问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM