c# - Getting stored procedure output parameter with Entity Framework is throwing a mapping error: The data reader is incompatible with the specified -
i working on legacy application , trying make call stored procedure returns integer getting mapping error. error one:
the data reader incompatible specified if_insert_incidents_citizen'. member of type, 'intid', not have corresponding column in data reader same name.
i tried change name _intid
, knowing entity framework translates names starts @variable
_variable
.
the weird thing if make use linqpad , make call stored procedure correct value of @intid
this stored procedure:
create procedure [dbo].[if_insert_incidents_citizen] @chvfolio varchar(50), @chvowner_name varchar(100), @chvowner_address varchar(100), @dtsdate_created smalldatetime, @intuser_id integer, @chvdescription varchar(250), @chvemail varchar(50), @intincident_type_id integer, @intincident_status integer, @intid int output set nocount on declare @interrorcode int declare @intapp_id int select @intincident_type_id = cast(param_value int) if_parameters param_name = 'citizen_incident_type_id' begin --get application id insert masterapplication (datecreated) values (getdate()) select @intapp_id = scope_identity() insert if_incidents ( folio, owner_name, owner_address, date_created, user_id, description, incident_type_id, incident_status, app_id ) values ( @chvfolio, @chvowner_name, @chvowner_address, @dtsdate_created, @intuser_id, @chvdescription , @intincident_type_id, @intincident_status, @intapp_id ) select @interrorcode = @@error, @intid = scope_identity() --insert complaint insert if_complaints ( incident_id, complainant_id, description , user_id, officer_assigned_id, complaint_status_id, date_made, email_complainant ) values ( @intid, null, @chvdescription + ' @ ' + @chvowner_address, 1, 1, 1, @dtsdate_created , @chvemail ) select @interrorcode = @@error--, @intid = scope_identity() end return @interrorcode
as can see in stored procedure, intid gets incremental value after insert, calling scope_identity()
now trying call stored procedure using entity framework following way:
var bptrep = dependencyfactory.getdependency<icetrepository<if_insert_incidents_citizen>>(); var parameters = getifinsertincidentscitizenparamenters(); var res = bptrep.getallfromstoredprocedure(storedprocedure, parameters);
the repository use class
//class return value public class if_insert_incidents_citizen { public int? intid { get; set; } }
here parameters stored procedure
private ienumerable<sqlparameter> getifinsertincidentscitizenparamenters() { // create parameters var parameters = new list<sqlparameter>(); var param1 = createsqlparameter("@chvfolio", sqldbtype.nvarchar, parameterdirection.input, criteria["chvfolio"].value, 50); parameters.add(param1); .... var paramx = createsqlparameter("@intincident_status", sqldbtype.int, parameterdirection.input, 10); parameters.add(paramx); var outparam = createsqlparameter("@intid", sqldbtype.int, parameterdirection.inputoutput, dbnull.value); parameters.add(outparam); } public iqueryable<tentity>getallfromstoredprocedure(string storedprocedure, ienumerable<sqlparameter> parameters) { var result = context.database.sqlquery<tentity>(storedprocedure, parameters).asqueryable(); return result; }
i followed approach @gertarnold suggested still not working, parameters , code
var returncode = new sqlparameter("@returncode", sqldbtype.int); returncode.direction = parameterdirection.output; var sql = "exec if_insert_incidents_citizen @chvfolio, @chvowner_name, @chvowner_address, @dtsdate_created, @intuser_id, @chvdescription, @chvemail, @intincident_type_id, @intincident_status, @intid out"; var data = ctx.database.sqlquery<object>(sql, returncode, parameters); var result = data.firstordefault();
finally @gertarnold put me on right direction, if haven't read post, please it, save lot of time stored procedures output parameters using sqlquery in dbcontext api
i added @intid out on exec call, there no need create returncode variable , call exec @returncode = ....
var returncode = new sqlparameter("@returncode", sqldbtype.int); returncode.direction = parameterdirection.output;
my code looks this, please aware call .toarray
method on parameters instead of passing collection of ienumerable
or ilist
method
var sql = "exec if_insert_incidents_citizen @chvfolio, @chvowner_name, @chvowner_address, @dtsdate_created, @intuser_id, @chvdescription, @chvemail, @intincident_type_id, @intincident_status, @intid out"; var data = ctx.database.sqlquery<object>(sql, returncode, parameters.toarray()); var result = data.firstordefault();
Comments
Post a Comment