在 Web api 控制器上调用方法时出错 - .Net Core 1.1 / EF 1.1
Error when calling method on web api controller - .Net Core 1.1 / EF 1.1
几个小时前,我 post 提出了一个问题,但是在去开会之前我急于 post 这个问题,我 post 提出了错误的东西。我已经删除了,这里是正确的版本:
我正在使用 EF Core 1.1 开发 .Net 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 只是一个标准 class 匹配数据库中相应的 table。数据库中有一所房子 - 而且只有一所房子 ID = 3.
知道我为什么会收到此错误吗?
更新:
这是错误的完整 StackTrace:
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, ISet1
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
您必须实例化您的上下文对象。尝试像这样进行 SingleOrDefault 调用:
using (var _context = new InspectionsContext())
{
var house = await _context.Houses.SingleOrDefaultAsync(m => m.HouseId == id);
}
好吧,看起来有些专栏 - 很可能是 Pomelo - 不喜欢。我删除了所有列并只保留了 3 个基本列(以及 int、bool 和 varchar)并且它有效。我将不得不尝试弄清楚它是哪一列...一旦我弄清楚,我会post...感谢大家的帮助
几个小时前,我 post 提出了一个问题,但是在去开会之前我急于 post 这个问题,我 post 提出了错误的东西。我已经删除了,这里是正确的版本:
我正在使用 EF Core 1.1 开发 .Net 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 只是一个标准 class 匹配数据库中相应的 table。数据库中有一所房子 - 而且只有一所房子 ID = 3.
知道我为什么会收到此错误吗?
更新:
这是错误的完整 StackTrace:
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
您必须实例化您的上下文对象。尝试像这样进行 SingleOrDefault 调用:
using (var _context = new InspectionsContext())
{
var house = await _context.Houses.SingleOrDefaultAsync(m => m.HouseId == id);
}
好吧,看起来有些专栏 - 很可能是 Pomelo - 不喜欢。我删除了所有列并只保留了 3 个基本列(以及 int、bool 和 varchar)并且它有效。我将不得不尝试弄清楚它是哪一列...一旦我弄清楚,我会post...感谢大家的帮助