且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用mysql使用输入和输出参数在Entity Framework Core中调用存储过程

更新时间:2023-09-10 21:19:40

我根据此问题,使用@ matt-g建议找到了解决方案。
为此,我必须使用ADO.net。

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
    }
}