Entity Framework and SCOPE_IDENTITY

前端 未结 3 1554
北海茫月
北海茫月 2021-01-21 22:09

I have a stored procedure that inserts into a table then executes this line

SET @returnVal = SCOPE_IDENTITY();

and after that I\'ve tried both:

3条回答
  •  醉话见心
    2021-01-21 22:55

    taken from the OPs question

    Adding an output parameter worked (answer marked below).

    The stored procedure signature looks like this now:

    My stored procedure signature now looks like this:

    CREATE PROCEDURE SP_MyStoreProc ([Multiple Parameters], @returnVal int output)
    

    The last line of the stored procedure is:

    return @returnVal
    

    My C# code looks like this now: (db is an instance of my dbContext class)

    System.Data.Objects.ObjectParameter identityParameter = 
    new System.Data.Objects.ObjectParameter("returnVal", 0);
    
    db.SP_MyStoredProc([Multiple Parameters], identityParameter);
    
    int myNewIdentity = Convert.ToInt32(identityParameter.Value);
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题