EntityFrameworkCore 5.05 ExecuteSqlRaw fail multiple update

James Bound 1 Reputation point
2021-07-23T16:45:49.02+00:00

I try to Execute a stored procedure from , if I execute it directly from SSMS, it works as expected, but if I use it from ExecuteSqlRaw or ExecuteSqlRawAsync, it fails as soon as it hits the first SELECT COUNT(), no error, but the second UPDATE never run.

An example of the stored procedure:

ALTER PROCEDURE [fact].[SetJobDoneByProcess]
     @JobId int
    ,@JobProcId int
    ,@JobUserId smallint
    ,@Done bit

AS
BEGIN

    DECLARE @procCnt int = 0 
           ,@procCntDone int = 0
           ,@CompletedDtTm datetime = NULL;

    --Update this process status
    IF @Done = 1
    BEGIN
        SET @CompletedDtTm = GETDATE();
    END

    UPDATE fact.Jobs_Processes
    SET CompletedDtTm = @CompletedDtTm, JobUserId = @JobUserId
    WHERE JobProcId = @JobProcId

    -- Compare job processes finished
    SET @CompletedDtTm = NULL;
    SET @procCnt = (SELECT COUNT(JobProcId) FROM fact.Jobs_Processes WHERE JobId = @JobId)
    SET @procCntDone = (SELECT COUNT(JobProcId) FROM fact.Jobs_Processes WHERE JobId = @JobId AND NOT CompletedDtTm IS NULL)

    -- Update job
    IF @procCnt = @procCntDone
    BEGIN
        SET @CompletedDtTm = GETDATE();
        UPDATE fact.Jobs
        SET CompletedDtTm = @CompletedDtTm
        WHERE JobId = @JobId
    END
    ELSE
    BEGIN
        UPDATE fact.Jobs
        SET StartedDtTm = GETDATE()
        WHERE JobId = @JobId
        AND StartedDtTm IS NULL
    END

END
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
741 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.