You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MVC Core 2存储过程输出参数返回null问题及Bug上报咨询

Hey there! Let's break this down step by step—first, let's rule out a common parameter binding mistake that's likely causing your issue, then we'll cover how to report a bug to the ASP.NET Core/EF Core team if it turns out to be a framework issue.

First: Fix Your Output Parameter Binding

Chances are, your problem isn't a framework bug—it's just a small misstep in how you're passing parameters to ExecuteSqlCommandAsync. Here's what's wrong and how to fix it:

  1. You're mixing string values and SqlParameter objects incorrectly: When using output parameters, you need to pass all parameters as SqlParameter instances, and explicitly map them to the stored procedure's parameter names.
  2. The -1 return value is normal: ExecuteSqlCommandAsync returns the number of rows affected by the command. If your stored procedure doesn't modify data (only returns an output parameter), it will return -1—this is expected behavior, not a bug.

Here's the corrected code:

// Define your input parameter with a matching name
var inputParam = new SqlParameter("@inputValue", "test");

// Define the output parameter with explicit direction and matching name
var outputTokenParam = new SqlParameter("@token", SqlDbType.String, 100)
{
    Direction = ParameterDirection.Output
};

// Execute the stored procedure, passing all parameters as SqlParameter objects
var resp = await _context.Database.ExecuteSqlCommandAsync(
    "EXEC dbo.yourstoredprocedure @inputValue, @token",
    inputParam,
    outputTokenParam
);

// Now you can access the output parameter's value
var tokenResult = outputTokenParam.Value?.ToString();

Key notes:

  • Make sure the parameter names (@inputValue, @token) exactly match the names defined in your stored procedure.
  • Always pass SqlParameter objects for all parameters (don't mix raw strings) so EF Core can properly bind parameter directions and types.

If It's Still a Bug: How to Report It to the EF Core Team

If you've confirmed that the stored procedure works correctly in SSMS, and raw ADO.NET (using SqlConnection/SqlCommand directly) returns the output parameter value, but EF Core 2.x still returns null, follow these steps to report the issue:

  • Step 1: Gather critical details:

    • Exact .NET Core 2.x version (e.g., 2.1.300)
    • Exact EF Core version (e.g., 2.1.14)
    • SQL Server version you're using
    • Full stored procedure definition
    • Minimal, runnable code that reproduces the issue (including your DbContext setup)
  • Step 2: Search existing issues:
    Head to the EF Core GitHub repository and search for keywords like "output parameter null ExecuteSqlCommandAsync" to see if someone has already reported the same problem. If there's an existing issue, you can add your details to it.

  • Step 3: Submit a new bug report:
    If no existing issue exists, click "New issue" and select the "Bug report" template. Fill in:

    • A clear title (e.g., "EF Core 2.1: Output parameter returns null when executing stored procedure via ExecuteSqlCommandAsync")
    • A detailed description of the problem, including your environment, reproduction steps, expected vs. actual results
    • A link to a minimal, reproducible project (e.g., a GitHub repo or zip file) so the team can quickly test your scenario

内容的提问来源于stack exchange,提问作者Ehi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:04:27