Dapper得到"指定的演员阵容无效".for ReturnValue参数值

use*_*300 5 c# stored-procedures dapper

我正在尝试使用SCOPE_IDENTITY将长主键返回到c#,使用DynamicParameter的ReturnValue选项.

以下是Dapper网站的示例代码:

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 

int b = p.Get<int>("@b");
int c = p.Get<int>("@c");
Run Code Online (Sandbox Code Playgroud)

我宁愿做以下操作,而不是返回int,因为我的主键字段应该是bigint

var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int64, direction: ParameterDirection.ReturnValue);

cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 

int b = p.Get<int>("@b");
int c = p.Get<long>("@c");
Run Code Online (Sandbox Code Playgroud)

在我的过程中,我正在使用"RETURN SCOPE_IDENTITY()".

但是,这样做似乎导致"指定演员表无效".例外.

Mar*_*ell 6

存储过程的返回值总是隐式为整数,即int.因此,您只能将其视为整数.如果您尝试将其拆箱long,则会失败.

选项:

  • 如果值适合,只需int在.NET端处理它
  • 否则使用out类型的参数bigint,并将其视为long在.NET端
  • 或使用selectQuery<long>(...).Single()