blazor with MySql EntityFrameworkCore return output parameter get error

0

i am using MySql.Data.EntityFrameworkCore with blazor and when i want to get output value total count for pagination from store procedure i get below error

Error: MySql.Data.MySqlClient.MySqlException (0x80004005): OUT or INOUT argument 1 for routine get_articles is not a variable or NEW pseudo-variable in BEFORE trigger

below code is my store procedure

DELIMITER $$
CREATE PROCEDURE get_articles(IN _offset INT, IN _count INT, OUT _total INT)
BEGIN
SELECT SQL_CALC_FOUND_ROWS * 
FROM content c JOIN content_types t 
ON c.content_type = t.id
WHERE t.name = 'article' 
LIMIT _offset, _count;
SET _total = FOUND_ROWS();
END$$
DELIMITER ;

below code is c#

var totalCount = new MySqlParameter()
{
ParameterName = "@TotalCount",
Direction = System.Data.ParameterDirection.Output, 
};

var earningTypes1 = await dbContext.Result.FromSqlRaw(
"CALL get_articles (0,30,@TotalCount)",
totalCount
).AsNoTracking().ToListAsync();
c#
mysql
asp.net-core
blazor
blazor-server-side
asked on Stack Overflow May 15, 2020 by user3903484 • edited May 18, 2020 by user3903484

2 Answers

0

Use CommandType.StoredProcedure instead of explicitly CALLing the stored procedure:

using (var command = new MySqlCommand("get_articles", dbConnection))
{
    command.CommandType = CommandType.StoredProcedure;
    var totalCount = new MySqlParameter()
    {
        ParameterName = "@TotalCount",
        Direction = ParameterDirection.Output, 
    };
    command.Parameters.Add(totalCount);

    using (var reader = await command.ExecuteReaderAsync())
    {
        // build list from results
    }

    // use totalCount.Value...
}

Note that if you're using async DB operations, you'll want to make sure you're using MySqlConnector as your ADO.NET library and Pomelo.EntityFrameworkCore.MySql for EF.Core, as they're the only MySQL libraries that support async I/O.

answered on Stack Overflow May 15, 2020 by Bradley Grainger
0

Since some versions of the Mysql connector do not support out parameters, the usual solution is to get the return value through MySQL user variables, and then use the query statement to get the variable value.

More details, refer to this.

Please confirm whether your current Mysql connector version supports the out parameter, you can refer to it.

answered on Stack Overflow May 17, 2020 by Yongqing Yu

User contributions licensed under CC BY-SA 3.0