Introduction
This article helps the developers who works on Entity Framework how to get the output parameter values from stored procedures using Entity Framework in various scenarios like last inserted or updated record ID.
Background
I have already explained how to use stored procedures in Entity Framework in my previous post.
So this is a successor to my previous article, please refer here.
Using the code
1) First of all we need to add an output parameter to a procedure to get the value as mentioned in below example.
Ex :-
CREATE PROCEDURE [dbo].[SP_InsertTestData]
(
@Value1 VARCHAR(20)
, @Value2 VARCHAR(MAX)
, @Value3 VARCHAR(50) = ''
, @LastInsertedRecordID INT OUTPUT
)
AS
BEGIN
**********
END
This procedure is created to insert one record to a table.
2) Now we need to set the value for this output parameter with last inserted record ID as below :-
SET @LastInsertedRecordID = SCOPE_IDENTITY()
3) Now we need to get this output parameter value in our code as below :-
using (this.objectContext = new TestEntities())
{
ObjectParameter objParam = new ObjectParameter("LastInsertedRecordID ", typeof(int));
var i = this.asterixContext.UpdateFullyPaidRecordStatus(value1, value2, value3, objParam);
this.objectContext.SaveChanges();
return Convert.ToInt32(objParam.Value);
}
Points of Interest
Hope this will benefit to those who started working with Entity Framework and want to use stored procedures with Entity Framework. Please contact me if any help you need from me.
Having more than 9 years of development experience in various Microsoft Technologies like :-
ASP.Net, MVC, SQL Server, WCF, MS Commerce Server, Xamarin etc.
Also hands on experience on the client side coding with JavaScript/jQuery and framework like AngularJS. Worked with third party ASP.net controls like Telerik and DevExpress as well.
Very much interested in Microsoft technologies to explore and implement. Now started to helping coders who really need me.
My Blog
Microsoft Technology Master
Programming Communities
JS Fiddle |
Stack Overflow
Awards
Microsoft MCTS Certification in ASP.Net 4.0