【问题标题】:Call stored procedure using pomelo.entityframeworkcore.mysql in .net core在 .net core 中使用 pomelo.entityframeworkcore.mysql 调用存储过程
【发布时间】:2018-10-20 09:23:15
【问题描述】:

我在 MySQL 中有 DB,使用常见的 mysql 实体框架最终会出错,所以我切换到 pomelo.entityframeworkcore.mysql。

在我的 MySQL Db 中,我的存储过程很少。但我不undertand如何与它沟通。通常的 mysql 查询可以完美运行并且可以正常工作:

public void ONELINE_SQL_TEST()
{
    var db = new DBContext();
    var test = db.DocumentsInfo.FromSql("SELECT * FROM `DocumentsInfo` LIMIT 0, 2").ToList();         
}

DocumentsInfo 是我的模型类/我的 mysql 表之一。

但我无法调用我的存储过程,不断收到错误消息:

USE `testProj`$$
CREATE PROCEDURE `GetDocumentsByName` (DocName varchar(255))
BEGIN
Select * from AllDocuments where DocumentName like DocName;
END$$

DELIMITER ;

和我尝试过的代码:

var docs1 = db.DocumentsInfo.FromSql("GetDocumentByName @DocName = {0}", Filename).ToList(); //not working
var docs2 = db.DocumentsInfo.FromSql("GetDocumentByName @p0", Filename).ToList(); //not working

错误:

您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 在第 1 行的 'GetDocumentByName 'nameAZ'' 附近

【问题讨论】:

    标签: mysql asp.net-core .net-core


    【解决方案1】:

    您需要在内存中的 sql 存储过程之前放置 CALL :)

    var docs1 = db.DocumentsInfo.FromSql("CALL GetDocumentByName @p0",    Filename).ToList();
    

    最后我读到 EF Core 不支持存储过程的命名参数,因此 @p0 因此,如果您有超过 1 个参数并说其中一个是您将执行以下操作的路径:

    var docs1 = db.DocumentsInfo.FromSql("CALL GetDocumentByName @p0, @p1",    parameters: new[] { Path, Filename }).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2013-11-23
      相关资源
      最近更新 更多