Dapper将C#decimal转换为SQL decimal而不是money(Always Encrypted)

时间:2017-05-22 02:24:04

标签: c# dapper always-encrypted

使用Dapper保存十进制数据(意味着进入AlwaysEncrypted货币字段)会导致此错误:

操作数类型冲突:使用(...)加密的十进制(5,1) 与用(...)

加密的钱不兼容

你如何明确告诉Dapper使用货币作为C#Decimal< - >类型的类型金钱是首选的映射?

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,) 

1 个答案:

答案 0 :(得分:0)

 SqlMapper.AddTypeMap(typeof(decimal), DbType.Currency); 

已将十进制的默认映射替换为DbType.Decimal,删除了问题的根源。