我有一个使用MYSQL数据库并需要从中调用存储过程的数据库优先MVC项目。我见过如何访问SQL数据库存储过程的示例,并试图将其复制并转换为使用MySql.Data.MySqlClient类(而不是System.Data.SqlClient类)。有人可以帮助我理解为什么我会得到下面的错误吗?我错过了什么...是否有一种不同的方法,我应该在MyDBContext方法中使用MYSQL,我错过了? (我没有发现在MVC中使用MYSQL存储过程的人太多,我知道这是一种罕见的情况,但真的需要这样做我的情况。所有堆栈溢出回答的问题我没有使用MySQL和MVC。 ...)MVC Database-first - 调用存储过程并出错
当我尝试调用db.GetContinentList()错误: “MySql.Data.MySqlClient.MySqlException:对程序dbName.ContinentListGet参数数目不正确;预计4,得到0”
我的代码...
控制器测试代码(只是通过结果作为测试循环,以确保它的工作原理):
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);
}