I have a table Student. This table has a column ID which is auto-increment. I want to write a stored procedure to add a new student and return the ID. I’m newbie at this.
This is my code. Please check if it’s wrong and fix for me and show me how to code to use it in C#. I used Entity Framework 4.
@Name NVARCHAR(50),
@Birthday DATETIME,
@ID bigint OUTPUT
AS
BEGIN
SET XACT_ABORT ON
BEGIN
INSERT INTO [Student](Name, Birthday) VALUES (@Name, @Birthday);
SELECT @ID = SCOPE_IDENTITY()
END
END
It is better you can
C#code insteadSPwhen your working withEF4.Using Entity Framework, this is all done automagically for you.
Saving in Entity Framework will automatically update the “auto-increment” ID column. You just read it out after the call to .SaveChanges();
EDIT:
Also read this post if you encounter any issues getting the “auto-increment” ID value.