mysql - MVC 数据库优先 - 调用存储过程并收到错误

标签 mysql asp.net-mvc stored-procedures dbcontext

我有一个数据库优先的 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 对象后调用存储过程的方式,这对我有用):

链接: 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()”作为命令文本,而且我传递参数的方式也不正确,需要进一步注意。我不认为这是重复的,因为另一个示例不是 MVC 数据库优先/DBContext 示例(这对于选择使用 MySQL 数据库使用 MVC 数据库优先的人可能有用)..但我让你们决定。

关于mysql - MVC 数据库优先 - 调用存储过程并收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46615312/

相关文章:

带有 IN 日期的 MySQL 过程失败

c# - 存储过程未返回输出参数

mysql - Mysql查询中的列字段Join

MYSQL 同一个表的多行根据条件产生多列

asp.net-mvc - 无法连接到远程服务器 - jQuery 是可能的,HttpWebRequest .NET 失败

c# - 你如何在 CaSTLe MVC (Monorail) 中获取用户的 IP 地址?

php - mysql中的存储过程用可变参数进行选择

mysql - MySQL 中可以同时有超过 1 个 sql_mode 吗?

当 Magento 同时被 300 个用户攻击时 mysql 消失了

c# - 如何在自定义验证属性中访问 View 模型的属性值以更改消息?