Ved
Ved

Reputation: 131

How to get / store return value from stored procedure call in Entity Framework?

Stored procedure:

CREATE PROCEDURE spIn  
    (@value1 varchar(100),
     @value2 varchar(100),)
AS
BEGIN
    INSERT INTO dbo.In(value1, value2,)
    VALUES (@value1, @value2,)
END
RETURN SCOPE_IDENTITY()
GO

In code it is like

using (DBEntities dataContext = new DBEntities())
{
    dataContext.spIn(value1,value2);
}

How to store the identity value??

FYI while function import I have given as Scalars of type Int32, if I check the column information no column is there.

Upvotes: 2

Views: 1043

Answers (1)

Ved
Ved

Reputation: 131

I have changed the stored procedure like this:

CREATE PROCEDURE spIn  
    (@value1 varchar(100),
     @value2 varchar(100),)
AS
BEGIN
    INSERT INTO dbo.In(value1, value2,)
    VALUES (@value1, @value2,)
END
SELECT @@IDENTITY AS id
GO

and the code change is as below

using (DBEntities dataContext = new DBEntities())
{
   int  = ID Convert.ToInt32(dataContext.spIn(value1,value2).FirstOrDefault().id); 
}

Upvotes: 1

Related Questions