使用Dapper保存十进制数据,这些数据旨在进入AlwaysCryptomoney字段会导致此错误:
操作数类型冲突:十进制 (5,1( 加密 (...(与用(...
您如何明确告诉 Dapper 使用货币作为类型,因为 C# 十进制 <-> 金钱是首选映射?
decimal income = 4500m;
using (var connection = new ReliableSqlConnection(connectionString)) {
try {
connection.Open();
connection.Query<int>(
@"INSERT INTO [tablename]
([MonthlyAfterTaxIncome])
VALUES
(@MonthlyAfterTaxIncome);
SELECT CAST(SCOPE_IDENTITY() as int)",
new {
MonthlyAfterTaxIncome = income
})
.FirstOrDefault();
} finally {
connection.Close();
}
}
列详细信息:
[MonthlyAfterTaxIncome] [money] ENCRYPTED WITH (COLUMN_ENCRYPTION_KEY = [KeyName], ENCRYPTION_TYPE = Deterministic, ALGORITHM = 'AEAD_AES_256_CBC_HMAC_SHA_256') NOT NULL,)
SqlMapper.AddTypeMap(typeof(decimal), DbType.Currency);
已替换 decimal 到 DbType.Decimal 的默认映射,删除了问题的根源。