如何使用 ExecuteNonQuery 检索范围标识?

How do I retrieve scope identity with ExecuteNonQuery?

我的项目正在使用 .NET Core 3.1,我的存储过程在我的存储库中执行 class。我想插入和 return 范围标识(刚刚插入的记录的 ID UserNumber),这样我就可以在同一方法中将它用于另一个存储过程。我这里的问题是 parameters[1].Value 值是 return 零。

这是我的存储过程的缩写:

ALTER PROCEDURE [dbo].[InsertUser]
@iUserNumber            int OUTPUT,
As
    INSERT dbo.tblUser (
        CreatedBy
        )
    VALUES          (@LoginUserId)
IF @@ERROR <> 0 GOTO ERRHANDLER
SET @UserNumber = SCOPE_IDENTITY() /** this is the primary Key **/
RETURN(@UserNumber)

这是我的存储库示例

public int InsertUsers(int LoginUserId, int UserNumber)
{
    SqlParameter[] parameters = new List<SqlParameter>()
    {
            _dbContext.CreateSqlParameter(StoredProcedureConstants.LoginUserId,SqlDbType.Int,LoginUserId.ToSafeInt()),
            _dbContext.CreateSqlParameter(StoredProcedureConstants.UserNumber,SqlDbType.Int,UserNumber.ToSafeInt())                
        }.ToArray();
        var intResult = _dbContext.ExecuteNonQuery(StoredProcedureConstants.InsertUsers, parameters);
        var result2 = parameters[1].Value; //This comes back as zero

如何将范围标识分配给 result2

应该是这样的:

CREATE OR ALTER PROCEDURE [dbo].[InsertUser]  
           @LoginUserId int,
           @iUserNumber int OUTPUT
As
    INSERT dbo.tblUser (CreatedBy)
    VALUES             (@LoginUserId)
    SET @iUserNumber = SCOPE_IDENTITY() /** this is the primary Key **/

SqlParameter[] parameters = new List<SqlParameter>()
{
   _dbContext.CreateSqlParameter("@LoginuserId",SqlDbType.Int,LoginUserId),
   _dbContext.CreateSqlParameter("@iUserNumber",SqlDbType.Int)                
 }.ToArray();
 parameters[1].Direction = ParameterDirection.Output;

 _dbContext.ExecuteNonQuery(StoredProcedureConstants.InsertUsers, parameters);
 var result2 = parameters[1].Value;