我正在尝试使用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");
我宁愿做以下操作,而不是返回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");
在我的过程中,我正在使用“RETURN SCOPE_IDENTITY()”。
但是这样做似乎导致“指定演员表无效”。例外。
存储过程的返回值总是隐式为整数,即int
。因此,您只能将其视为整数。如果您尝试将其拆分为long
,则会失败。
选项:
int
bigint
类型的out
参数,并在.NET端将其视为long
select
和Query<long>(...).Single()
如果我没记错的话,SCOPE_IDENTITY()会返回一个小数( http://msdn.microsoft.com/en-us/library/ms190315.aspx ),因此会产生无效的强制转换异常。
您需要将它转换为bigint(在SQL中)才能使其按您的需要工作。