c# - Error when calling method on web api controller - .Net Core 1.1 / EF 1.1 -
a few hours ago posted question, in haste post question before going meeting, posted wrong thing. have deleted it, , here correct version:
i developing .net core 1.1 web api using ef core 1.1, connects mysql database using pomelo. connects properly, when try read record:
http://localhost:50082/api/houses/3
i following error:
an unhandled exception occurred while processing request. argumentoutofrangeexception: index out of range. must non-negative , less size of collection. parameter name: index system.throwhelper.throwargumentoutofrange_indexexception()
here controller looks like:
private readonly inspectionscontext _context; // get: api/houses/5 [httpget("{id}")] public async task<iactionresult> gethouse([fromroute] int? id) { if (!modelstate.isvalid) { return badrequest(modelstate); } var house = await _context.houses.singleordefaultasync(m => m.houseid == id); if (house == null) { return notfound(); } return ok(house); }
the error happens on line:
var house= await _context.houses.singleordefaultasync(m => m.houseid == id);
house standard class matching corresponding table in database. , there house - , 1 house - in database id = 3.
any ideas why i'm getting error?
update:
here full stacktrace of error:
at system.throwhelper.throwargumentoutofrange_indexexception() @ system.szarrayhelper.get_item[t](int32 index) @ lambda_method(closure , valuebuffer ) @ microsoft.entityframeworkcore.changetracking.internal.internalmixedentityentry..ctor(istatemanager statemanager, ientitytype entitytype, object entity, valuebuffer valuebuffer) @ microsoft.entityframeworkcore.changetracking.internal.internalentityentryfactory.newinternalentityentry(istatemanager statemanager, ientitytype entitytype, object entity, valuebuffer valuebuffer) @ microsoft.entityframeworkcore.changetracking.internal.internalentityentryfactory.create(istatemanager statemanager, ientitytype entitytype, object entity, valuebuffer valuebuffer) @ microsoft.entityframeworkcore.changetracking.internal.statemanager.starttrackingfromquery(ientitytype baseentitytype, object entity, valuebuffer valuebuffer, iset
1 handledforeignkeys) @ microsoft.entityframeworkcore.query.internal.entitytrackinginfo.starttracking(istatemanager statemanager, object entity, valuebuffer valuebuffer) @ microsoft.entityframeworkcore.query.internal.querybuffer.starttracking(object entity, entitytrackinginfo entitytrackinginfo) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.<>c__displayclass16_0
2.<_trackentities>b__0(tout result) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.selectasyncenumerable2.selectasyncenumerator.<movenext>d__4.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw() @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ microsoft.entityframeworkcore.query.internal.asynclinqoperatorprovider.exceptioninterceptor
1.enumeratorexceptioninterceptor.d__5.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw() @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ inspectionswebapi.controllers.housescontroller.d__4.movenext() in c:\users\fabsr\source\repos\inspectionswebapi\inspectionswebapi\controllers\housescontroller.cs:line 46
you have instantiate context object. try making singleordefault call like:
using (var _context = new inspectionscontext()) { var house = await _context.houses.singleordefaultasync(m => m.houseid == id); }
Comments
Post a Comment