【问题标题】:Dapper and user defined database typesDapper 和用户定义的数据库类型
【发布时间】:2017-02-14 08:17:16
【问题描述】:

假设我在数据库中有一个用户定义的数据类型: MySuperType 其中有两个 bigint 属性(Id + Code)

我有一个接受这些参数的存储过程:

@MySuperType, @Price, @dateModified, etc...

现在在我的代码中,我像这样使用 Dapper:

using (var connecion = new SqlConnection(_connectionString))
{
    result = connection.Execute("SP_name", 
                new {mySuperType, price, date}, 
                commandType: CommandType.StoredProcedure);
}

但我总是收到错误

类型操作数冲突:Dapper 无法将用户定义类型:MySuperType 从参数映射到存储过程。

更改存储过程或替换用户定义类型不是一种选择

有谁知道我可以做些什么来映射类型并将参数(所有参数)发送到存储过程?

【问题讨论】:

    标签: c# asp.net sql-server dapper


    【解决方案1】:

    参数名称必须与过程中定义的参数相对应:

    using (var connecion = new SqlConnection(_connectionString))
            {
                result = connection.Execute("SP_name", 
                    new {MySuperType = mySuperType, Price = price, dateModified = date}, 
                    commandType: CommandType.StoredProcedure);
            }
    

    Dapper 在 2 个方向(作为参数和作为结果)的映射中区分大小写。

    如果您想避免指定参数名称,请确保在 c# 和存储过程中的变量中具有完全相同的名称。

    【讨论】:

      猜你喜欢
      • 2012-03-23
      • 2013-04-03
      • 1970-01-01
      • 2014-12-31
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      相关资源
      最近更新 更多