c# - 从代码调用时Mysql存储过程结果不同

标签 c# mysql entity-framework

我正在使用 EF 6.0 和 Mysql。我的存储过程是

delimiter $$
use ge_wv8g73$$
CREATE PROCEDURE Get_UserMapSelections(IN p_userId int)
BEGIN

SELECT @DefaultMap := ValueLong FROM userselections WHERE section = 'Last 
Selections' and userid = p_userId and Description like 'LastSelectedMapID';

SELECT CONCAT(

(SELECT IFNULL( 
@DefaultMap ,'0')
) ,',', (

SELECT ValueLong FROM userselections where section = 'Last Selections' and userid = p_userId
and Description like concat('MapPage', @DefaultMap)),',',(

SELECT ValueLong FROM userselections where section = 'Last Selections' and userid = p_userId
and Description like concat('MapRow', @DefaultMap)),',',(

SELECT ValueLong FROM userselections where section = 'Last Selections' and userid = p_userId
and Description like concat('MapCol', @DefaultMap))) as selections;

END$$

当我在 mysql 工作台中调用它时,它返回不同的结果

59,147,8,579

但是当我使用 EF 通过代码调用它时,结果仅为 59

GE_Context.cs

 public string Get_UserMapSelections(int userId)
    {
        StringBuilder spCommand = new StringBuilder();

        MySqlParameter[] mySqlParams = new MySqlParameter[] { new MySqlParameter("p_userId", userId)
                                                            };

        spCommand.Append("CALL Get_UserMapSelections(@p_userId);");

        try
        {
            return this.Database.SqlQuery<string>(spCommand.ToString(), mySqlParams).First();
        }
        catch { return null; }
    }

可能是什么问题?有时,当我使用与表中的字段名称相同的参数名称时,例如,如果我使用了

select * from maps where userid = userid

但这里的情况并非如此。

最佳答案

您的 EF 框架代码:

return this.Database.SqlQuery(spCommand.ToString(), mySqlParams).First();

您仅返回第一行。因此您只得到 1 个结果

关于c# - 从代码调用时Mysql存储过程结果不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47384326/

相关文章:

c# - 我可以在 c# 中使用带有 switch case 的正则表达式吗?

c# - 信号量和 Web 套接字

mysql - 从 2 个表中选择获取包含外键为空的行

mysql - 从 MYSQL 数据库中选择子记录的有效方法

c# - 如何从实体序列化为json?

c# - 将新订阅者添加到 mailchimp 中的列表

c# - 包含半个元组

MySQL 连接帖子、用户信息和评级的三个表

c# - 批量插入和更新性能 C# "ADO.NET"Vs "Linq2SQL"Vs "EF-DataFirst approach"

c# - 在哪里可以找到有关在真实项目中使用 Entity Framework 的良好分步教程?