sql - EF 4.1 Code First: Execute Stored Proc and return IDENT -
i'm attempting use context.database
execute stored proc inserts record , returns identity. know proc works, using either output parameter, select, or return, cannot either executesqlcommand
or sqlquery<int>
return identity.
using stored proc return,
insert tablea (fielda, fieldb) return scope_identity
i see return value of 1010966
ssms
declare @return_value int exec @return_value = spinsertrecord select 'return value' = @return_value
but nothing have done in application returns valid value.
int64 result = context.database.executesqlcommand
always returns 3 , casting error referencing decimal when using sqlquery<int>
. made attempt output parameter, wasn't able code execute properly, have focused on app consuming either select scope_identity
or return scope_identity
short of creating models replace logic contained in stored procedure, i'm open , suggestions returning value both stored proc , application level.
here how can work. in example, entitymanager class inherits dbcontext.
test table:
create table [dbo].[tablea]( [id] [int] identity(1,1) not null, [fielda] [nvarchar](50) null, [fieldb] [nvarchar](50) null ) on [primary]
stored proc:
create proc spinsertrecord @fielda nvarchar(50), @fieldb nvarchar(50) insert tablea (fielda, fieldb) values (@fielda, @fieldb) select cast(@@identity int)
calling code (vb.net)
public shared function addrecord(fielda string, fieldb string) integer try using em new entitymanager return em.database.sqlquery(of integer)( "exec spinsertrecord @fielda, @fieldb", new sqlparameter("fielda", fielda), new sqlparameter("fieldb", fieldb)).singleordefault end using catch ex exception throw new applicationexception("an error occurred while executing addrecord", ex) end try end function
consuming code:
return addrecord("tom", "halladay")
my result incrementing return value. hope helps.
Comments
Post a Comment