为什么EF Core总是使用此存储过程返回-1? [英] Why does EF Core always return -1 with this stored procedure?

查看:102
本文介绍了为什么EF Core总是使用此存储过程返回-1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对本地2016 DB使用EF Core(最新版本),并且每次都返回-1.我不知道我做错了什么?

I am trying to use EF Core (latest release) against a Local 2016 DB and I am getting -1 back every time. I don't know what I have done wrong?

我知道它正在到达数据库.我检查了.

I know that it is reaching the database. I checked that.

int returnCode = _dbContext.Database.ExecuteSqlCommand("CheckReceivedNotificationDuplicate @p0, @p1, @p2", 
parameters: new[] { sendMessage.MESSAGE_TEMPLATE_NAME, sendMessage.MESSAGE_SUBJECT, sendMessage.MESSAGE_TEXT_SUMMARY });

存储过程:

ALTER PROCEDURE [dbo].[CheckReceivedNotificationDuplicate]
@MESSAGE_TEMPLATE_NAME nvarchar(100),
@MESSAGE_SUBJECT nvarchar(255),
@MESSAGE_TEXT_SUMMARY nvarchar(4000)
AS  DECLARE @NMID Int
BEGIN   
IF  EXISTS (SELECT 1 FROM dbo.Notification WHERE MESSAGE_TEMPLATE_NAME = @MESSAGE_TEMPLATE_NAME
                and MESSAGE_SUBJECT = @MESSAGE_SUBJECT AND ReceiveTimeEST > dateadd(minute,-5, dbo.GetGMTtoEST(getutcdate())))
BEGIN 
    IF  EXISTS (SELECT 1 FROM dbo.Notification WHERE MESSAGE_TEMPLATE_NAME = @MESSAGE_TEMPLATE_NAME
                and MESSAGE_SUBJECT = @MESSAGE_SUBJECT AND MESSAGE_TEXT_SUMMARY = @MESSAGE_TEXT_SUMMARY
                AND ReceiveTimeEST > dateadd(minute,-5, dbo.GetGMTtoEST(getutcdate())))
        RETURN -99
    ELSE
        RETURN 0
END
ELSE
    RETURN 0
END

推荐答案

ExecuteSqlCommand调用 IRelationalCommand.ExecuteNonQuery .它返回受影响的行数.

ExecuteSqlCommand calls IRelationalCommand.ExecuteNonQuery internally. It returns the number of rows affected.

要从存储过程中返回任意值,请将输出参数添加到其声明和ExecuteSqlCommand调用中:

To return an arbitrary value from stored procedure, add output parameter to its declaration and to ExecuteSqlCommand call:

var outParameter = new SqlParameter("@outParameter", DbType.Int32)
{
    Direction = ParameterDirection.Output
};

context.Database.ExecuteSqlCommand("exec GetFoo @outParameter OUT", outParameter);

这篇关于为什么EF Core总是使用此存储过程返回-1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆