从.NET DB上下文调用MySQL存储过程将抛出“只能存储MySqlParameter对象". [英] Calling MySQL Stored Procedure from .NET DB Context throws "Only MySqlParameter objects may be stored'"
问题描述
我正在使用.NET Core 5 DBContext访问MYSQL存储过程以使用以下代码获取结果:
I am using .NET Core 5 DBContext to access MYSQL Stored procedure to get results with the below code :
var param = new SqlParameter("@word", word);
var result = _context.History.FromSqlRaw("EXEC GETHISTORY @word", param).ToList();
if (result != null)
{
retval = (IEnumerable<HistoryDM>)result;
}
但是它会引发以下异常,
Yet it throws the below exception,
MySql.Data.MySqlClient.MySqlException:仅可以存储MySqlParameter对象"
MySql.Data.MySqlClient.MySqlException: 'Only MySqlParameter objects may be stored'
那么,试图告诉我的错误是什么,我该如何解决?
So, what is the error trying to tell me and how can I fix it ?
推荐答案
好的,看起来好像很简单:
OK, looks like when one knows it it is relatively simple :
Object result = _context.History.FromSqlInterpolated($"CALL ADDHISTORY ({word})").ToList();
因此,使用CALL(而不是EXEC)和在参数周围加上括号,这也类似于MySQL本机调用.这里没有黑魔法.
So, both CALL is used (instead of EXEC) and parentheses around the parameter, which is also like the MySQL native call. No black magic here.
非常感谢@Marc的支持!
Thanks a lot for your support @Marc !!
这篇关于从.NET DB上下文调用MySQL存储过程将抛出“只能存储MySqlParameter对象".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!