几个小时前,我发布了一个问题,但在去会议之前匆忙发布问题,我发布了错误的内容。我已经删除了,这是正确的版本:
我正在使用 EF Core 1.1 开发一个 .Net Core 1.1 Web API,它使用 Pomelo 连接到 MySQL 数据库。它连接正确,但是当我尝试读取某个记录时:
http://localhost:50082/api/houses/3
我收到以下错误:
处理请求时发生未处理的异常。ArgumentOutOfRangeException:索引超出范围。必须是非负数且小于集合的大小。参数名称:索引 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 的数据库中有一所房子——而且只有一所房子。
任何想法为什么我会收到此错误?
更新:
这是错误的完整 StackTrace:
在 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.Create(IStateManager stateManager, IEntityType entityType, Object entity) 在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer) , ValueBuffer valueBuffer) 在 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() --- 从先前抛出异常的位置结束堆栈跟踪---在系统.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 InspectionsWebApi.Controllers.HousesController.d__4.MoveNext()在 C:\Users\fabsr\Source\Repos\InspectionsWebApi\InspectionsWebApi\Controllers\HousesController.cs:line 46