我有一个数据库优先的 MVC 项目,它使用 MYSQL 数据库,需要从中调用存储过程。我已经看到了如何访问 SQL 数据库存储过程的示例,并尝试将其复制并将其转换为使用 MySql.Data.MySqlClient 类(而不是 System.Data.SqlClient 类)。有人可以帮助我理解为什么我会收到以下错误吗?我是否遗漏了一些东西...对于我遗漏的 MYSQL,我应该在下面的 MyDBContext 方法中使用不同的方法吗? (我还没有发现太多人在 MVC 中使用 MYSQL 存储过程,我知道这是一种罕见的情况,但对于我的情况确实需要这个。我看到的所有堆栈溢出回答的问题都没有将 MySQL 与 MVC 一起使用。谢谢...)
当我尝试调用 db.GetContinentList() 时出现错误:“MySql.Data.MySqlClient.MySqlException:PROCEDURE dbName.ContinentListGet 的参数数量不正确;预期为 4,得到 0”
我的代码...
Controller 测试代码(只需循环遍历结果作为测试以确保其有效):
private MyDbContext db = new MyDbContext();
public ActionResult Test()
{
var continentList = db.GetContinentList(1, "M", 1, 0);
var result = "";
foreach (sp_ContinentList c in continentList)
{
result = result + c.Common_Name + "(" + c.Scientific_Name + ")<br />";
}
return Content(result);
}
模型已创建:(我确保它与存储过程结果集列的输出匹配)
public class sp_ContinentList
{
public int ID { get; set; }
public string Common_Name { get; set; }
public string Scientific_Name { get; set; }
public int Profile { get; set; }
public string Area { get; set; }
}
MyDBContext.cs代码:
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyDbContextConnectionString")
{
Database.SetInitializer<MyDbContext>(new MyDbInitializer());
}
public virtual ObjectResult<sp_ContinentList> GetContinentList(int continent, string group, int profile, int createCache)
{
MySqlParameter continentParam = new MySqlParameter("@vContinent", MySqlDbType.Int16);
continentParam.Direction = ParameterDirection.Input;
continentParam.Value = continent;
MySqlParameter groupParam = new MySqlParameter("@vGroup", MySqlDbType.VarChar, 50, group);
groupParam.Direction = ParameterDirection.Input;
MySqlParameter profileParam = new MySqlParameter("@vProfile", MySqlDbType.Int16);
profileParam.Direction = ParameterDirection.Input;
profileParam.Value = profile;
MySqlParameter createCacheParam = new MySqlParameter("@vCreateCache", MySqlDbType.Int16);
createCacheParam.Direction = ParameterDirection.Input;
createCacheParam.Value = createCache;
MySqlParameter[] spParams = new MySqlParameter[] {
continentParam, groupParam, profileParam, createCacheParam
};
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<sp_ContinentList>("ContinentListGet", spParams);
}
最佳答案
我能够从下面的堆栈溢出链接找到答案(它不是使用 MVC,而是通过模仿 MomentSurfer 在构建 MySqlParameter 对象后调用存储过程的方式,这对我有用):
public virtual ObjectResult<sp_ContinentList> GetContinentList(Nullable<int> continent, string group, Nullable<int> profile, Nullable<int> createCache)
{
MySqlParameter continentParam = new MySqlParameter("vContinent", MySqlDbType.Int16);
continentParam.Direction = ParameterDirection.Input;
continentParam.Value = continent;
MySqlParameter groupParam = new MySqlParameter("vGroup", MySqlDbType.VarChar, 50);
groupParam.Direction = ParameterDirection.Input;
groupParam.Value = group;
MySqlParameter profileParam = new MySqlParameter("vProfile", MySqlDbType.Int16);
profileParam.Direction = ParameterDirection.Input;
profileParam.Value = profile;
MySqlParameter createCacheParam = new MySqlParameter("vCreateCache", MySqlDbType.Int16);
createCacheParam.Direction = ParameterDirection.Input;
createCacheParam.Value = createCache;
MySqlParameter[] spParams = new MySqlParameter[] {
continentParam, groupParam, profileParam, createCacheParam
};
// New/Additional code needed:
StringBuilder sb = new StringBuilder();
sb.Append("CALL ContinentListGet(@vContinent, @vGroup, @vProfile, @vCreateCache)");
string commandText = sb.ToString();
return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreQuery<sp_ContinentList>(commandText, spParams);
}
我没有意识到我实际上必须说“CALL sproc_name()”作为命令文本,而且我传递参数的方式也不正确,需要进一步注意。我不认为这是重复的,因为另一个示例不是 MVC 数据库优先/DBContext 示例(这对于选择使用 MySQL 数据库使用 MVC 数据库优先的人可能有用)..但我让你们决定。
关于mysql - MVC 数据库优先 - 调用存储过程并收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46615312/