Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在web api控制器-.Net Core 1.1/EF 1.1上调用方法时出错_C#_Mysql_Entity Framework Core_Asp.net Core Webapi_Pomelo Entityframeworkcore Mysql - Fatal编程技术网

C# 在web api控制器-.Net Core 1.1/EF 1.1上调用方法时出错

C# 在web api控制器-.Net Core 1.1/EF 1.1上调用方法时出错,c#,mysql,entity-framework-core,asp.net-core-webapi,pomelo-entityframeworkcore-mysql,C#,Mysql,Entity Framework Core,Asp.net Core Webapi,Pomelo Entityframeworkcore Mysql,几个小时前,我发布了一个问题,但在我赶在开会前发布问题时,我发布了错误的内容。我已将其删除,以下是正确的版本: 我正在使用EFCore1.1开发一个.NETCore1.1WebAPI,它使用Pomelo连接到一个MySQL数据库。 它连接正确,但当我尝试读取某个记录时: 我得到以下错误: 处理请求时发生未处理的异常。 ArgumentOutOfRangeException:索引超出范围。必须是 非负数且小于集合的大小。参数名称: 索引系统.ThrowHelper.ThrowArgumentOu

几个小时前,我发布了一个问题,但在我赶在开会前发布问题时,我发布了错误的内容。我已将其删除,以下是正确的版本:

我正在使用EFCore1.1开发一个.NETCore1.1WebAPI,它使用Pomelo连接到一个MySQL数据库。 它连接正确,但当我尝试读取某个记录时:

我得到以下错误:

处理请求时发生未处理的异常。 ArgumentOutOfRangeException:索引超出范围。必须是 非负数且小于集合的大小。参数名称: 索引系统.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);
        }
House只是一个与数据库中相应表匹配的标准类。数据库中有一所房子,而且只有一所房子,ID=3

知道我为什么会犯这个错误吗

更新:

以下是错误的完整堆栈跟踪:

在System.ThrowHelper.ThrowArgumentOutOfRange_IndexException()处 System.szarrayhelp.get_Item[T](Int32索引)位于 lambda_方法(闭包、值缓冲区) Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntry..ctor(IStateManager stateManager、entityType entityType、对象实体、ValueBuffer valueBuffer)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager、entityType entityType、对象实体、ValueBuffer valueBuffer)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.Create(IStateManager stateManager、entityType entityType、对象实体、ValueBuffer valueBuffer)在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType,对象实体,ValueBuffer ValueBuffer,ISet
1
手持外国钥匙)在
Microsoft.EntityFrameworkCore.Query.Internal.EntityTrackingInfo.StartTracking(IStateManager
stateManager、对象实体、ValueBuffer(ValueBuffer ValueBuffer)位于
Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.StartTracking(对象
实体,EntityTrackingInfo EntityTrackingInfo)位于
Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.c\uu DisplayClass16\u0
2.b\u0(TOut 结果)在 Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable
2.SelectAsyncEnumerator.d_u4.MoveNext()
---来自引发异常的上一个位置的堆栈结束跟踪---在
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
任务)在
Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor
1.EnumeratorExceptionInterceptor.d_u5.MoveNext() ---来自引发异常的上一个位置的堆栈结束跟踪---在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()位于 System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中 在 InspectionsWebApi.Controllers.HousesController.d_u4.MoveNext() 在里面 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);
}

好吧,看起来是某个专栏,很可能是柚子,不喜欢。我删除了所有列,只保留了3个基本列(以及int、bool和varchar),并且它可以正常工作。我得试着找出是哪一列。。。一旦我弄明白了,我会发布。。。谢谢大家的帮助

每家每户都失败了,还是只有那一家失败了?Id可以为null:int?id。所以如果你得到一个空id,查询可能会失败。有效吗?你能发布完整的异常调用堆栈吗?谢谢你的快速回复。。。是的,它在任何房屋中都会失败,并显示相同的错误消息。我检查了控制器中的Id参数,它填充了值“3”。我试过了,但没用。我将通过编辑原始问题发布完整的异常堆栈。再次感谢。。。
using (var _context = new InspectionsContext()) 
{     
    var house = await _context.Houses.SingleOrDefaultAsync(m => m.HouseId == id);
}