为什么我的存储过程中的输出参数是从 Entity Null 调用的?

Why is my output parameter in stored procedure called from Entity Null?

我正在调用一个存储过程并声明一个输出参数:

CREATE PROCEDURE dbo.usp_getIsReadyForProcess
@VideoId INT ,
@val INT OUTPUT
AS
BEGIN
BEGIN TRY
    BEGIN TRANSACTION
-- LOCK ROW UNTIL END OF TRANSACTION
        SELECT * FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = @VideoId

然后在整个交易过程中设置值

  --If there is no row count
IF @@ROWCOUNT = 0
BEGIN
    SET @val = 0
END

-- If video is already in process
ELSE IF @statusCode > 1
BEGIN
    SET @val = 0
END

...... more if blocks

          -- RELEASE LOCK
    COMMIT TRANSACTION
END TRY   

这里是获取输出参数的c#代码:

        using (var db = EntityFactory.GetInstance())
        {
            ObjectParameter objParam = new ObjectParameter("val", typeof(int));
            db.usp_getIsReadyForProcess(videoId, objParam);

            return (int)objParam.Value == 1;
        }

...但是无论我做什么,objParam.Value 都是空的。

所以我更深入地挖掘并发现了一个已经处理过的异常,

留言: 只能在 Type.IsGenericParameter 为真的类型上调用方法。

我做错了什么?我尝试了 type(bool, int32, string) .. 没有任何效果

您的存储过程 returns 结果集,由于这一行:

SELECT * FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = @VideoId

您可以将 SQL 更改为类似以下内容:

DECLARE @RowCount INT
SELECT @RowCount = COUNT(*) FROM dbo.ProcessStatus WITH (ROWLOCK, HOLDLOCK) WHERE VideoId = @VideoId

或者您可以在代码中捕获结果集:

var resultSet = db.usp_getIsReadyForProcess(videoId, objParam);