EF Core调用存储过程提示@soruceCurrencyId参数未提供,如何解决?
问题原因及解决方法
核心问题是参数名拼写不匹配:
- 存储过程定义的参数是
@soruceCurrencyId(单词拼写错误,少了字母c) - 调用代码中传入的参数是
@sourceCurrencyId(正确拼写为source)
SQL Server会严格匹配参数名称,这就导致存储过程找不到预期的@soruceCurrencyId参数,从而抛出错误。
修正步骤
- 统一参数名称
推荐修正存储过程的参数名,使用正确拼写,避免后续调用再出问题:
ALTER PROCEDURE [dbo].[GetCurrencyAtDate] @sourceCurrencyId int, -- 修正拼写为source @targetCurrencyId int, @year int, @month int, @day int, @result decimal OUTPUT AS BEGIN DECLARE @targetDate DATETIME SET @targetDate = DATEADD(DAY, @day - 1, DATEADD(MONTH, @month - 1, DATEADD(YEAR, @year - 1900, 0))) SELECT TOP 1 @result = Currency FROM KurTable WHERE SourceKur = @sourceCurrencyId -- 同步修正此处的参数引用 AND TargetKur = @targetCurrencyId AND Date <= @targetDate ORDER BY ABS(DATEDIFF(DAY, Date, @targetDate)); END
- 修正输出参数的获取逻辑
你的代码用ExecuteScalarAsync()获取结果,但存储过程是通过OUTPUT参数返回值的,ExecuteScalar会返回查询的第一行第一列,而此处查询仅做赋值操作,不会返回结果集,正确做法是执行命令后读取输出参数的值:
// 替换原有的ExecuteScalarAsync代码 await command.ExecuteNonQueryAsync(); currencyValue = Convert.ToDecimal(resultParameter.Value);
内容的提问来源于stack exchange,提问作者Musa AKYUZ
相关产品推荐
相关产品推荐

