使用实体框架导入函数时无法获取输出参数
这是我的 SQL Server 存储过程:
ALTER PROCEDURE [dbo].[SearchUser]
(@Text NVARCHAR(100),
@TotalRows INT = 0 OUTPUT)
AS
BEGIN
SELECT @TotalRows=1000
SELECT * from Users
END
和我的 C# 代码
using (var context = new TestDBEntities())
{
var outputParameter = new ObjectParameter("TotalRows", typeof(Int32));
context.SearchUser("", outputParameter);
Response.Write(outputParameter.Value);
}
但是 outputParameter.Value
始终为 null。
有人能告诉我为什么吗?
Here's my SQL Server stored procedure :
ALTER PROCEDURE [dbo].[SearchUser]
(@Text NVARCHAR(100),
@TotalRows INT = 0 OUTPUT)
AS
BEGIN
SELECT @TotalRows=1000
SELECT * from Users
END
And my C# code
using (var context = new TestDBEntities())
{
var outputParameter = new ObjectParameter("TotalRows", typeof(Int32));
context.SearchUser("", outputParameter);
Response.Write(outputParameter.Value);
}
However outputParameter.Value
always is null.
Could anybody tell me why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
存储过程执行期间输出参数由其实际值填充。
但是表值存储过程实际上仅在您尝试迭代结果记录集而不是调用包装器方法时才会执行。
所以,这不起作用:
这起作用:
当您使用不返回任何记录集的存储过程时,它们会在方法调用后立即执行,因此您在输出参数中有实际值。
Output parameters filled by its actual values during the execution of the stored procedure.
But table-valued stored procedure actually get executed only in moment when you're trying to iterate resulting recordset, but not calling a wrapper method.
So, this DOES'T work:
This DOES:
When you're working with stored procedures what don't return any recordset, they execute immediately after a method call, so you have actual value in output parameter.
我以前也有同样的问题。我认为实体框架存在错误的主要原因是用户存储过程有输出参数并返回结果集。例如:
但是,如果您按如下方式更改用户存储过程,您可以获得输出参数
您可以按如下方式解决方法:
如果有人有更好的解决方案,请告诉我
I have same problem before. The main reason I think that the entities framework has the bug in case the user stored procedure has output parameter and return a result set. For example:
However if you change the user stored procedure as following, you can get the output params
You can workaround as following:
If anybody has better solution, please tell me
由于延迟执行,我们的单元测试失败了,我们遇到了类似的问题。简而言之,如果您有一个不返回任何内容的存储过程,请确保将响应类型设置为“无”,当设置为“无”时,它将在调用时执行,而不是延迟。
如果您返回任何内容(例如字符串结果的标量类型),当您使用结果时,即使 .Count() 或 .ToList() 位于包含函数调用的方法之外,它也会执行它 >。
所以不需要的时候尽量不要强制执行,需要的时候应该执行,但是一定要声明正确,否则可能会不起作用。
We had a simular issue due to defered excecution our unit tests failed. In short if you have a stored proc that does NOT return anything you need to be sure to set the response type as 'None' when set as 'None' it will be excecuted when called and not defered.
In case you return anything (E.g. Scalar type of String results) it will excecute it when you use the result even if that .Count() or .ToList() is outside of the method that contains the function call.
So try not to force excecution if not need, when needed it should excecute but be sure to declare it correctly or it might not work.