[英]Entity Framework and SCOPE_IDENTITY
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:之后我都尝试过:
SELECT @returnVal
and和
return @returnVal
When I execute the stored procedure from Microsoft SQL Server Management Studio, I get the expected result with SELECT @returnVal
- the identity column for the inserted data is selected.当我从 Microsoft SQL Server Management Studio 执行存储过程时,我使用
SELECT @returnVal
获得预期结果 - SELECT @returnVal
了插入数据的标识列。
However when I add the stored procedure to my ADO.Net Entity Data Model / EntityFramework class / .edmx class and execute the stored procedure in my C# code, I get the value -1 returned without fail.但是,当我将存储过程添加到我的 ADO.Net 实体数据模型 / EntityFramework 类 / .edmx 类并在我的 C# 代码中执行存储过程时,我得到的值 -1 没有失败返回。
Is it possible to get the value that I want, the new identity value, returned?是否有可能获得我想要的值,即返回的新标识值?
I realize that I could manually bind the stored procedure to the insert action of the table in my model - but this is not an option.我意识到我可以手动将存储过程绑定到模型中表的插入操作 - 但这不是一个选项。 There are far too many insert procedures to do this manual work every time I regenerate my model class(es).
每次我重新生成我的模型类时,有太多的插入过程来完成这项手动工作。
Declare a output type of parameter in your procedure definition:在过程定义中声明参数的输出类型:
create procedure [dbo].[Procedurename] @returnVal int output
as
SET @returnVal = SCOPE_IDENTITY();
and while calling the stored procedure call it as:并在调用存储过程时将其调用为:
declare @returnVal int
exec Procedurename @returnVal output
print @returnVal
context.Database.ExecuteSqlCommand
return the command executing result and not the query result. context.Database.ExecuteSqlCommand
返回命令执行结果,而不是查询结果。 If you need to get data than use context.Database.SqlQuery
.如果您需要获取数据而不是使用
context.Database.SqlQuery
。
SET @ReturnVal=SCOPE_IDENTITY()
and then use the select. SET @ReturnVal=SCOPE_IDENTITY()
然后使用选择。
Example: How to use DbContext.Database.SqlQuery<TElement>(sql, params) with stored procedure?示例: 如何将 DbContext.Database.SqlQuery<TElement>(sql, params) 与存储过程一起使用? EF Code First CTP5
EF Code First CTP5
taken from the OPs question取自 OP 问题
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)我的 C# 代码现在看起来像这样:(db 是我的 dbContext 类的一个实例)
System.Data.Objects.ObjectParameter identityParameter =
new System.Data.Objects.ObjectParameter("returnVal", 0);
db.SP_MyStoredProc([Multiple Parameters], identityParameter);
int myNewIdentity = Convert.ToInt32(identityParameter.Value);
If you open the edmx file and right click on the function import in the model browser you can tell entity framework that the stored procedure returns a collection of scalars.如果您打开 edmx 文件并右键单击模型浏览器中的函数导入,您可以告诉实体框架存储过程返回一个标量集合。 Then you can essentially call the sproc with mycontext.mysproc().Single() to get the ID.
然后,您基本上可以使用 mycontext.mysproc().Single() 调用 sproc 以获取 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.