簡體   English   中英

如何使用 mysql 使用輸入和 output 參數在 Entity Framework Core 中調用存儲過程

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

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. 我可以在 MySQL 工作台中調用它

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

現在我想使用實體框架核心來調用它,我目前使用的代碼是

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;

我目前得到的例外是

CommandType 為 Text 時僅支持 ParameterDirection.Input(參數名稱:@PageCount)

你可以試試下面的東西。

  1. 使用 exec 而不是調用

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

  2. 存儲過程中的 Select PageCount

我從這個 github問題中得到了信息。

我根據這個問題使用@matt-g 建議找到了解決方案。 我不得不為此使用 ADO.net 作為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM