Why does the Entity Framework call my stored procedure but return the wrong value?

I have a stored procedure that simply returns the total number of records divided by any value that is passed. This will help with pagination on the website.

However, I use an entity structure to bind to this stored procedure, and it returns -1for all calls. When I interrogate a stored procedure using SQL Management Studio, it returns with the correct value.

My stored procedure is as follows:

CREATE PROCEDURE [dbo].[GetAuditRecordPageCount]
@Count INTEGER
AS
RETURN ((SELECT COUNT(Id) FROM AuditRecords) / @Count) + 1

And my call to entity structure in C # is as follows:

int pageCount;
using (Entities entities = new Entities())
{
    pageCount = entities.GetAuditRecordPageCount(count);
}

Did I write C # code correctly this way?

As requested in the SQL comments generated by EF:

exec [dbo].[GetAuditRecordPageCount] @Count=100
+1
1

? http://www.devtoolshed.com/using-stored-procedures-entity-framework-scalar-return-values

, :

CREATE PROCEDURE [dbo].[GetAuditRecordPageCount]
@Count INTEGER
AS
declare @retVal int
set @retVal = ((SELECT COUNT(Id) FROM AuditRecords) / @Count) + 1
select @retVal

#:

int? pageCount;
using (Entities entities = new Entities())
{
   pageCount = entities.GetAuditRecordPageCount(count).SingleOrDefault();

   if (pageCount.HasValue)
   {
      //do something here
   }
   else
   {

   }
}

"Scalars: Int32" .

+3

Source: https://habr.com/ru/post/1544825/


All Articles