几个小时前我发布了一个问题,但是在出席会议之前急于发布问题,我发布了错误的信息。我删除了它,这是正确的版本:
我正在使用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:
at System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()at System.SZArrayHelper.get_Item [T](Int32索引)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.&lt; _TrackEntities&gt; 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.ExceptionInterceptor
1.EnumeratorExceptionInterceptor.d__5.MoveNext() ---从抛出异常的先前位置开始的堆栈跟踪结束--- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 InspectionsWebApi.Controllers.HousesController.d__4.MoveNext() 在 C:\ Users \用户fabsr \源\回购\ InspectionsWebApi \ InspectionsWebApi \控制器\ HousesController.cs:线 46
答案 0 :(得分:1)
您必须实例化您的上下文对象。尝试将您的SingleOrDefault调用为:
using (var _context = new InspectionsContext())
{
var house = await _context.Houses.SingleOrDefaultAsync(m => m.HouseId == id);
}
答案 1 :(得分:0)
好吧,看起来像是一些专栏 - 很可能是柚子 - 不喜欢。我删除了所有列,只保留了3个基本列(和int,bool和varchar),它可以工作。我必须弄清楚它是哪一个栏目......一旦我弄明白,我就会发帖...感谢大家的帮助