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

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)

解决方案

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();

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值