c# - How to work with SQL Server stored procedures using Entity Framework -
i'm new programming using stored procedures , entity framework. have created sql server database table called dbo.book
hold book title , auto incremental id column.
i have created stored procedure insert record dbo.book
, return id
column value of newly entered record:
use [testing1db] go set ansi_nulls on go set quoted_identifier on go create procedure dbo.uspaddbook @title nvarchar(50), @bookid int output begin set nocount on; insert dbo.book (title) values (@title) select @bookid = @@identity end
when move visual studio , add entity framework data model c# windows forms project using existing database, generates ...context.cs
code related stored procedure follows:
public virtual int uspaddbook(string title, objectparameter bookid) { var titleparameter = title != null ? new objectparameter("title", title) : new objectparameter("title", typeof(string)); return ((iobjectcontextadapter)this).objectcontext.executefunction("uspaddbook", titleparameter, bookid); }
my problem is, method requires 2 parameters. 1 title can pass code, , parameter denotes 'bookid' don't know is, because wanted pass title of book , book_id
in return. so, can't call method 'uspaddbook' because don't know send second parameter. please me.
you set objectparameter
...
objectparameter bookid = new objectparameter("bookid", typeof(int32));//check type context.uspaddbook("title", bookid); int newbookid = bookid.value;
or, can use ef this....
context.book.addobject(title); context.savechanges(); int bookid = title.bookid;
i haven't tested of this, it's along right lines...
Comments
Post a Comment