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

2024-04-24

我使用 ASP.net Core 2.2 与 Entity Framework core 2.2.6 和 Pomelo.EntityFrameworkCore.MySql 2.2.0 来连接 MySQL,我有一个存储过程,它需要 3 个输入参数和 1 个输出参数。我可以在 MySQL Workbench 中调用它,例如

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 而不是 call

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

  2. 在存储过程中选择PageCount

我从这个github获取信息issue https://github.com/mysql-net/MySqlConnector/issues/231.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 mysql 使用输入和输出参数调用 Entity Framework Core 中的存储过程 的相关文章

随机推荐