How to call stored procedure in Entity Framework Core with input and output parameters using mysql

entity-framework-core input-parameters mysql output-parameter stored-procedures

Question

I am using ASP.net Core 2.2 with Entity Framework core 2.2.6 and Pomelo.EntityFrameworkCore.MySql 2.2.0 for connectivity with MySQL, I have a stored procedure which takes 3 input parameters and 1 output parameter. I am able to call it in MySQL workbench like

CALL GetTechniciansByTrade('Automobile', 1, 10, @total);
select @total;

Now I want to call this using entity framework core, the code I am currently using is

var outputParameter = new MySqlParameter("@PageCount", MySqlDbType.Int32);
outputParameter.Direction = System.Data.ParameterDirection.Output;

var results = await _context.GetTechnicians.FromSql("Call GetTechniciansByTrade(@MyTrade, @PageIndex, @PageSize, @PageCount OUT)",
new MySqlParameter("@MyTrade", Trade),
new MySqlParameter("@PageIndex", PageIndex),
new MySqlParameter("@PageSize", PageSize),
outputParameter).ToListAsync();

int PageCount = (int)outputParameter.Value;

Exception I am getting currently is

Only ParameterDirection.Input is supported when CommandType is Text (parameter name: @PageCount)

1
2
10/16/2019 7:47:41 PM

Accepted Answer

I found the solution using @matt-g suggestion based on this Question. I had to use ADO.net for this as

var technicians = new List<TechnicianModel>();
using (MySqlConnection lconn = new MySqlConnection(_context.Database.GetDbConnection().ConnectionString))
{
    lconn.Open();
    using (MySqlCommand cmd = new MySqlCommand())
    {
        cmd.Connection = lconn;
        cmd.CommandText = "GetTechniciansByTrade"; // The name of the Stored Proc
        cmd.CommandType = CommandType.StoredProcedure; // It is a Stored Proc

        cmd.Parameters.AddWithValue("@Trade", Trade);
        cmd.Parameters.AddWithValue("@PageIndex", PageIndex);
        cmd.Parameters.AddWithValue("@PageSize", PageSize);

        cmd.Parameters.AddWithValue("@PageCount", MySqlDbType.Int32);
        cmd.Parameters["@PageCount"].Direction = ParameterDirection.Output; // from System.Data

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                technicians.Add(new TechnicianModel()
                {
                    City = reader["City"].ToString(),
                    ExperienceYears = reader["ExperienceYears"] != null ? Convert.ToInt32(reader["ExperienceYears"]) : 0,
                    Id = Guid.Parse(reader["Id"].ToString()),
                    Name = reader["Name"].ToString(),
                    Qualification = reader["Qualification"].ToString(),
                    Town = reader["Town"].ToString()
                });
            }
        }

        Object obj = cmd.Parameters["@PageCount"].Value;
        var lParam = (Int32)obj;    // more useful datatype
    }
}
0
10/20/2019 3:46:48 PM

Popular Answer

Can you try below things.

  1. Use exec instead of call

    var results = await _context.GetTechnicians.FromSql("EXEC GetTechniciansByTrade(@MyTrade, @PageIndex, @PageSize, @PageCount OUTPUT)"

  2. Select PageCount in stored procedure

I got information from this github issue.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow