无法使用实体框架读取从存储过程返回的值
本文关键字:存储过程 返回 读取 框架 实体 | 更新日期: 2023-09-27 17:53:37
我已经编写了一个存储过程,如果找到,将返回行ID,如果没有找到,将创建行并返回新创建行的ID。我在c#函数中检索这个返回值时遇到了问题。
存储过程:
PROCEDURE [TagDatabase].[GetTagID]
-- Add the parameters for the stored procedure here
@TagName as varchar(200),
@UserID int = 0,
@TagID int = 0 OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select @TagID=ID from Tag where Name = @TagName and UserID=@UserID;
IF NULLIF(@TagID, '') IS NULL
BEGIN
DECLARE @OutputTbl TABLE (ID INT)
INSERT INTO Tag(Name, ParentID, UserID)
OUTPUT INSERTED.ID INTO @OutputTbl(ID)
VALUES (@TagName, 0, @UserID);
select @TagID=ID from @OutputTbl
END
select @TagID
END
我的函数来检索这个返回值:
ObjectParameter objParam = new ObjectParameter("TagID", typeof(long));
context.GetTagID("newTag", 10, objParam);
context.SaveChanges();
long id = Convert.ToInt64(Convert.ToInt64(id)));
变化
long id = Convert.ToInt64(Convert.ToInt64(id));
long id = Convert.ToInt64(objParam.Value);