I have this for a stored procedure which outputs just an integer
ALTER PROC GetPendingReservations
AS
BEGIN
SELECT
COUNT(DISTINCT FacilityAndAmenityId )
FROM
[Property].[PropAndAmenReservation] with(nolock)
WHERE
IsGranted = 'False'
AND CAST(StartDate AS DATE) = CAST(GetDate() AS DATE)
AND FacilityAndAmenityId NOT IN (SELECT FacilityAndAmenityId
FROM [Property].[PropAndAmenReservation] with(nolock)
WHERE IsGranted = 'True'
AND CAST(StartDate AS DATE) = CAST(GetDate() AS DATE))
END
and I call it in my web api through this.
[HttpGet]
[Route("api/getfacilitiesreservation")]
public int GetFacilitiesReservation()
{
using(var db = new ApplicationDbContext())
{
try
{
const string query = "GetPendingReservations";
var count = db.Database.SqlQuery<int>(query);
return count;
}
catch(Exception ex)
{
return 0;
}
}
}
But I get this error in my catch exception:
The specified cast from a materialized 'System.Int32' type to the 'System.String' type is not valid.
If I use db.Database.SqlQuery<int>(query)
, this is what I get:
Can you please show me how to this right? Thank you.