首页 > 解决方案 > ExecuteSqlCommand 返回 -1?

问题描述

我试图从代码中调用存储过程,在 ef core 2 中我发现我可以使用context.database.ExecuteSqlCommand()函数来执行我的数据库中的任何存储过程。

虽然存储过程在我的 sql server 实例中运行良好:

在此处输入图像描述 从我的代码中调用它,返回 -1 结果!?:

  using (var context = new AZPDBDEMOGZContext(optionsBuilder.Options))
        {

            var existUser = context.Database.ExecuteSqlCommand("GetIdUsager_ByNomPrenomEmail @p0,@p1,@p2", parameters:new[] { "rouarouarouarouaroua", "wiem", "test@gmail.com" });

        }

什么mi兴奋剂错了?

标签: ef-core-2.1

解决方案


ExecuteSqlCommand 只会为 DML 查询(即插入/更新/删除)返回“受影响的行”整数。您的存储过程看起来像是在返回一个结果集,因此它返回 -1,因为您没有提供更改数据源的语句。您需要创建一个 POCO 并使用 FromSql 来填充它。


推荐阅读