【问题标题】:Get the returned value of Stored Procedure in EF6获取EF6中存储过程的返回值
【发布时间】:2026-01-13 01:20:03
【问题描述】:

我的 SQL Server 数据库中有一个存储过程,用作并发控制。

例如:

IF row is deleted concurrently 
   RETURN 0
ELSE IF row is edited concurrently 
   RETURN -1
ELSE 
   RETURN 1

当我这样做时使用 C#:

int i = myContext.SP(params);

似乎存储过程只返回 0 和 1,它从不返回 -1 值(我在 SQL Server Management Studio 中运行存储过程时得到 -1,所以没问题)。

我认为这可能不是获取存储过程返回值的正确方法。这怎么可能?

【问题讨论】:

  • @user2864740:我的意思是无论返回值是什么,它都只返回0和1
  • 我相信 EF 不可能做到这一点
  • @DavidG:如果这是真的,那就太可悲了
  • @CamBruce 这是实体框架,不是 ADO
  • 参数将只是 Sp 采用的值,例如 int/string/datetime/etc 值。

标签: c# entity-framework stored-procedures


【解决方案1】:

实体框架问题需要实体框架解决方案。在这种情况下,我才意识到我也可以这样做:

 //Run the SP
 Context.SP(params...,ID, Version);
 //Check the result 
 //Concurrently Deleted
 var C_Deleted = Context.Table.Where(x=> x.ID.Equals(ID)).Count() <= 0;
 //Concurrently Edited
 var C_Edited = (from x in Context.Table
                where x.ID.Equals(ID)
                && 
                x.Version == Version
                select x).Count() <= 0; 

【讨论】: