Monday, 15 February 2010

c# - Use a stored procedure in entity framework -



c# - Use a stored procedure in entity framework -

i have model-first ef model. imported first stored procedure: cpas_poidvendorprojectdate

i imported function. has 3 input parameters: @projectid(int), @vendorid(int), , @workdate(datetime), , returns @poid(int).

here's sql code:

create procedure [dbo].[cpas_poidvendorprojectdate] @vendorid int, @projectid int, @workdate datetime, @po_id int output begin set nocount on; declare @rowcount int; select @po_id = id tblpo vendorid = @vendorid , expirationdate >= @workdate , (projectid null or projectid = @projectid) , capitalexpense = ( select capitalexpense tblprojects id=@projectid) , groupcode in (1,3,5); set @rowcount = @@rowcount; if (@rowcount != 1) set @po_id = -1*@rowcount; end

i called in c# programme follows:

context.cpas_poidvendorprojectdate( currvendorid, currprojectid, currworkdate, currpoid);

intellisense says utilize of "context" wrong...it's "variable", , i'm using "method".

in addition, currpoid rejected because it's looking system.data.objects.ojbectparameter, not int. intellisense happy function name , other parameters (strangely...)

what doing wrong here?

you can if nil else works:

using(var context = new mydatacontext()) { using(var cmd = context.database.connection.createcommand()) { cmd.commandtext = "cpas_poidvendorprojectdate"; cmd.commandtype = commandtype.storedprocedure; //if stored proc accepts params, here pass them in cmd.parameters.add(new sqlparameter("vendorid", 10)); cmd.parameters.add(new sqlparameter("projectid", 12)); cmd.parameters.add(new sqlparameter("workdate", datetimw.now)); var poid = (int)cmd.executescalar(); } }

c# entity-framework stored-procedures

No comments:

Post a Comment