简体   繁体   English

EF5从存储过程中收集数据,该存储过程返回列表或不返回任何内容

[英]EF5 collecting data from stored procedure that returns a list or nothing

I have a stored procedure that looks like this: 我有一个看起来像这样的存储过程:

CREATE PROCEDURE [dbo].[Foo]
(
  @ID INT,
  @PARAM1 NVARCHAR(10),
  @Error INT OUT
)
AS 
BEGIN
  IF (@ID < 0)
  BEGIN
    SET @Error = 1
    RETURN
  END

  IF (@ID > 100)
  BEGIN
    SET @Error = 2
    RETURN
  END

  SELECT Field1, Field2, Field3 FROM TABLE WHERE Param = @PARAM1
END

In EF5, I'm calling it like this 在EF5中,我这样称呼它

ObjectParameter error = new ObjectParameter("Error", typeof(global::System.Int32));
List<MyFoo> list = db.Foo(id, param1, error).ToList();

The code does not compile because "NOT ALL PATH RETURNS A VALUE" which makes sense since I have a couple of "RETURN" calls in the stored procedure. 该代码无法编译,因为"NOT ALL PATH RETURNS A VALUE"很有意义,因为我在存储过程中有几个"RETURN"调用。

QUESTION: 题:
What's the right way to call this stored procedure from EF5 (cannot change the stored procedure)? 从EF5调用此存储过程的正确方法是什么(无法​​更改存储过程)?

Thanks in advance 提前致谢

Remove the Error parameter and RETURN s from the SP and use RAISERROR to signal errors. 从SP中删除Error参数和RETURN ,并使用RAISERROR表示错误。 Make sure that severity is greater than 10 so it would throw an SqlException, but less than 17 so it's not a system problem which is reported to DB admin: 确保严重性大于10,因此将引发SqlException,但小于17,因此这不是系统问题,已向数据库管理员报告:

CREATE PROCEDURE [dbo].[Foo]
(
  @ID INT,
  @PARAM1 NVARCHAR(10)
)
AS 
BEGIN
  IF (@ID < 0)
  BEGIN
    RAISERROR('ID is less than 0', 16, 0); 
  END

  IF (@ID > 100)
  BEGIN
    RAISERROR('ID is greater than 100', 16, 0); 
  END

  SELECT Field1, Field2, Field3 FROM TABLE WHERE Param = @PARAM1
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM