幾個小時前我發佈了一個問題,但是在開會前我急於發佈問題,我發佈了錯誤的東西。我刪除了它,並且這裏是正確的版本:在web api控制器上調用方法時出現錯誤 - .Net Core 1.1/EF 1.1
我正在開發一個使用EF Core 1.1的Web核心API,它使用Pomelo連接到MySQL數據庫。 它連接正常,但是當我嘗試讀取某個記錄:
http://localhost:50082/api/houses/3
我收到以下錯誤:
An unhandled exception occurred while processing the request. ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()
這裏是我的控制器看起來像:
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);
}
錯誤發生在這條線上:
var house= await _context.Houses.SingleOrDefaultAsync(m => m.HouseId == id);
House只是一個匹配數據庫中相應表的標準類。並且在ID = 3的數據庫中有一間房子 - 只有一間房子。
任何想法爲什麼我得到這個錯誤?
UPDATE:
這裏是錯誤的完整堆棧跟蹤:
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException() at System.SZArrayHelper.get_Item[T](Int32 index) at lambda_method(Closure , ValueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntityEntry..ctor(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.Create(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer valueBuffer, ISet
1 handledForeignKeys) at Microsoft.EntityFrameworkCore.Query.Internal.EntityTrackingInfo.StartTracking(IStateManager stateManager, Object entity, ValueBuffer valueBuffer) at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.StartTracking(Object entity, EntityTrackingInfo entityTrackingInfo) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.<>c__DisplayClass16_0
2.<_TrackEntities>b__0(TOut result) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable2.SelectAsyncEnumerator.<MoveNext>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor
1.EnumeratorExceptionInterceptor.d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at InspectionsWebApi.Controllers.HousesController.d__4.MoveNext() in C:\Users\fabsr\Source\Repos\InspectionsWebApi\InspectionsWebApi\Controllers\HousesController.cs:line 46
它失敗了,每個房子,或只是與那一個? – Hackerman
Id可以爲null:int? ID。所以如果你有一個空ID,查詢可能會失敗。 – jdweng
http:// localhost:50082/api/houses?id = 3是否有效? – bsantisi