为什么实体框架调用我的存储过程但返回不正确的值? [英] Why is Entity Framework calling my stored procedure but returning an incorrect value?
问题描述
我有一个存储过程,该过程只返回记录总数除以传入的值。这有助于网站上的分页。
I have a stored procedure that simply returns the total number of records divided by whatever value is passed in. This is to aid in pagination on a website.
但是,我正在使用实体框架绑定到该存储过程,并且对它的所有调用都返回 -1
。当我使用SQL Management Studio询问存储过程时,它会返回正确的值。
However, I am using the entity framework to bind to that stored procedure and it's returning -1
for all calls to it. When I interrogate the stored procedure using SQL Management Studio, it comes back with the correct value.
我的存储过程如下:
CREATE PROCEDURE [dbo].[GetAuditRecordPageCount]
@Count INTEGER
AS
RETURN ((SELECT COUNT(Id) FROM AuditRecords) / @Count) + 1
我在C#中对实体框架的调用是这样的:
And my call to the entity framework in C# is this:
int pageCount;
using (Entities entities = new Entities())
{
pageCount = entities.GetAuditRecordPageCount(count);
}
我是否以这种方式编写C#代码?
Am I correct in writing the C# code this way?
根据注释中的请求,EF生成的SQL为:
As per a request in the comments, the SQL generated by EF is:
exec [dbo].[GetAuditRecordPageCount] @Count=100
推荐答案
你有尝试过吗?
http://www.devtoolshed.com / using-stored-procedures-entity-framework-scalar-return-values
我认为您的过程将如下所示:
I think your procedure will look like this:
CREATE PROCEDURE [dbo].[GetAuditRecordPageCount]
@Count INTEGER
AS
declare @retVal int
set @retVal = ((SELECT COUNT(Id) FROM AuditRecords) / @Count) + 1
select @retVal
并在C#代码中:
int? pageCount;
using (Entities entities = new Entities())
{
pageCount = entities.GetAuditRecordPageCount(count).SingleOrDefault();
if (pageCount.HasValue)
{
//do something here
}
else
{
}
}
别忘了在编辑功能中添加 Scalars:Int32导入屏幕。
Don't forget to put "Scalars: Int32" in edit function Import screen.
这篇关于为什么实体框架调用我的存储过程但返回不正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!