I'm having trouble calling stored procs in ASP.NET Core 3.0. Some of the ones I've called have worked, but I found one that won't, and I haven't seen a lot of online documentation that addresses this (in 3.0, uses ExecuteSqlInterpolatedAsync and ExecuteSqlRaw in place of methods such as FromSql).
The stored procedure is a simple one (it just calls another sproc and returns on int):
ALTER PROCEDURE [dbo].[getsystemnumber](@p_controlid varchar(8),
@p_alternateid varchar(8))
AS
DECLARE @return_number int
-- Requests a block of 1 number
EXECUTE @return_number = dbo.getsystemnumber_gateway @p_controlid, @p_alternateid, 1
RETURN @return_number
It's a long standing sproc that is used daily by older applications and works fine, and if I run it manually it works fine.
I'm trying to create a simple method that can be called whenever the application needs a new Id
public async Task<int> getNewId()
{
using (TMW_LiveContext context = new TMW_LiveContext())
{
FormattableString query = $"EXEC dbo.getsystemnumber @p_controlid = 'fgtbycmp', @p_alternateid = ''";
CancellationToken cancellationToken = default;
int newId = await context.Database.ExecuteSqlInterpolatedAsync(query, cancellationToken);
return newId;
}
}
Which is called like so
int newId = getNewId().Result;
The problem I am encounter is that is always returns a value of 1, no exceptions. I have also tried the non-asynchronous call (context.Database.ExecuteSqlInterpolated(query)) and using ExecuteSqlRaw. I always get 1 as a result. But if I run the sproc in SQL server, I get the values I expect (values looking like this: 13798303). What am I doing wrong? How can I get it to return the proper integer values?
EDIT - One thing to note is that the sproc does two things: It returns the sys_controlnumber from systemcontrol and then in increments sys_controlnumber. The latter is working, which is good, so I know it's calling the sproc, but I can never get it to return the sys_controlnumber, which is equally important.