为什么我在调用存储过程时会出现异常?

时间:2016-08-23 10:30:06

标签: c# .net entity-framework

我正在使用Entity Framework 6和Microsoft SQL Server 2012。

这是我的存储过程:

ALTER PROCEDURE SPDeleteRegion
      @siteId int,
      @regionId int 
AS
BEGIN 
    SET NOCOUNT ON;

    DECLARE @isDeleted BIT 

    IF EXISTS (SELECT 1 FROM SiteObjects WHERE SiteRegionId = @regionId)
    BEGIN
        SET @isDeleted = 0 ; --not deleted
        RETURN @isDeleted;
    END
    ELSE
    BEGIN
        --do what needs to be done if not       
        DELETE FROM SiteRegions 
        WHERE Id = @regionId;

        SET @isDeleted = 1;  -- deleted
        RETURN @isDeleted;
    END
END

这里我如何在C#中调用存储过程:

var t = _context.Database.SqlQuery<bool>("SPDeleteRegion @siteId, @regionId",
                                          new SqlParameter("@siteId", siteId),
                                          new SqlParameter("@regionId", regionId));

在上面的代码行中,我得到了这个例外:

  

数据阅读器有多个字段。多个字段对EDM原语或枚举类型无效。

我知道为什么会得到这样的解释以及如何修复它?

1 个答案:

答案 0 :(得分:2)

您的程序没有选择任何内容。改变它:

ALTER PROCEDURE SPDeleteRegion
    -- Add the parameters for the stored procedure here
      @siteId int,
      @regionId int 
AS
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE 
    @isDeleted BIT 

    IF EXISTS (SELECT 1 FROM SiteObjects WHERE SiteRegionId = @regionId)
    BEGIN
        SET @isDeleted = 0 ; --not deleted
        SELECT @isDeleted [IsDeleted]; --HERE
    END
    ELSE
    BEGIN
        --do what needs to be done if not       
        DELETE FROM SiteRegions WHERE Id = @regionId;
        SET @isDeleted = 1;--deleted
        SELECT @isDeleted [IsDeleted]; -- AND HERE
    END
END