【发布时间】:2017-09-02 22:45:57
【问题描述】:
几个小时前,我发布了一个问题,但在去开会之前匆忙发布问题,我发布了错误的内容。我已经删除了,这里是正确的版本:
我正在使用 EF Core 1.1 开发一个 .Net Core 1.1 Web API,它使用 Pomelo 连接到 MySQL 数据库。 它连接正确,但是当我尝试读取某个记录时:
http://localhost:50082/api/houses/3
我收到以下错误:
处理请求时发生未处理的异常。 ArgumentOutOfRangeException:索引超出范围。一定是 非负数且小于集合的大小。参数名称: 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 的数据库中有一所房子——而且只有一所房子。
知道为什么我会收到此错误吗?
更新:
这里是错误的完整 StackTrace:
在 System.ThrowHelper.ThrowArgumentOutOfRange_IndexException() 在 System.SZArrayHelper.get_Item[T](Int32 索引)在 lambda_method(闭包,ValueBuffer)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntityEntry..ctor(IStateManager stateManager、IEntityType entityType、对象实体、ValueBuffer 值缓冲区)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager、IEntityType entityType、对象实体、ValueBuffer 值缓冲区)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.Create(IStateManager stateManager、IEntityType entityType、对象实体、ValueBuffer 值缓冲区)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType,对象实体,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_02.<_trackentities>b__0(TOut 结果)在 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.ExceptionInterceptor1.EnumeratorExceptionInterceptor.d__5.MoveNext() --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 InspectionsWebApi.Controllers.HousesController.d__4.MoveNext() 在 C:\Users\fabsr\Source\Repos\InspectionsWebApi\InspectionsWebApi\Controllers\HousesController.cs:line 46
【问题讨论】:
-
每个房子都失败了,还是只有那一个?
-
Id 可以为 null : int? ID。因此,如果您的 id 为空,查询可能会失败。
-
你能发布完整的异常调用堆栈吗?
-
感谢您的快速回复...是的,任何房子都失败了,并显示相同的错误消息。我检查了控制器中的 Id 参数,它填充了值“3”。我试过localhost:50082/api/houses?id=3,但没用。我将通过编辑原始问题发布完整的异常堆栈。再次感谢...
标签: c# mysql entity-framework-core asp.net-core-webapi pomelo-entityframeworkcore-mysql