使用输出参数执行存储过程后,实体框架6 ObjectParameter为空

时间:2018-06-14 23:04:39

标签: entity-framework entity-framework-6

因此,我正在尝试从Entity Framework 6执行SP并返回输出参数@AffectedRowCount,该参数包含受执行影响的行数。下面是我的代码和SSMS执行的屏幕截图,显示了正确的结果。每次通过C#执行时,rowCount.Value为null,即使在重置数据后也是如此。有什么想法吗。

    public int UpdateStagingTable()
    {
        var rowCount = new ObjectParameter("AffectedRowCount", typeof(Int32));
        be.sp_LoanCategoryMonitor(rowCount);
        return Convert.ToInt32(rowCount.Value);
    }

这是dbContext的自动代码

public virtual ObjectResult<sp_LoanCategoryMonitor_Result> sp_LoanCategoryMonitor(ObjectParameter affectedRowCount)
    {
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<sp_LoanCategoryMonitor_Result>("sp_LoanCategoryMonitor", affectedRowCount);
    }

SSMS执行

DECLARE @return_value int,
        @AffectedRowCount int

EXEC    @return_value = [dbo].[sp_LoanCategoryMonitor]
        @AffectedRowCount = @AffectedRowCount OUTPUT

SELECT  @AffectedRowCount as N'@AffectedRowCount'

SELECT  'Return Value' = @return_value

SSMS结果

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

我发现了这个问题。无论出于何种原因创建SP的开发人员在SP的最后一个语句中添加了一个select语句。这干扰了输出参数。我更新了SP,然后清理了SP的.edmx文件,然后将其添加回来。我运行它并按预期运行。

enter image description here