2017-10-06 71 views
0

我有一个使用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); 
    } 

回答

0

我能够从下面的堆栈溢出链接找到答案(它不是使用MVC,而是模仿MomentS urfer的调用buildling在了MySQLParameter对象后存储过程的方式,这个工作对我来说):

链接: Using the entity framework with a MySQL DB and the model designer doesn't pickup stored proc parameters

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() “作为命令文本,而我通过params的方式也是不正确的,需要进一步关注。我不认为这是重复的,但因为另一个例子不是MVC Database-first/DBContext的例子(这可能对那些选择使用MVC数据库的人有用 - 首先使用MySQL数据库)..但是我会让你们决定。