2017-04-07 20 views
0

幾個小時前我發佈了一個問題,但是在開會前我急於發佈問題,我發佈了錯誤的東西。我刪除了它,並且這裏是正確的版本:在web api控制器上調用方法時出現錯誤 - .Net Core 1.1/EF 1.1

我正在開發一個使用EF 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只是一個匹配數據庫中相應表的標準類。並且在ID = 3的數據庫中有一間房子 - 只有一間房子。

任何想法爲什麼我得到這個錯誤?

UPDATE:

這裏是錯誤的完整堆棧跟蹤:

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.SelectAsyncEnumerable 2.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

+0

它失敗了,每個房子,或只是與那一個? – Hackerman

+0

Id可以爲null:int? ID。所以如果你有一個空ID,查詢可能會失敗。 – jdweng

+0

http:// localhost:50082/api/houses?id = 3是否有效? – bsantisi

回答

1

你必須實例化上下文對象。嘗試使您的SingleOrDefault調用如:

using (var _context = new InspectionsContext()) 
{  
    var house = await _context.Houses.SingleOrDefaultAsync(m => m.HouseId == id); 
} 
0

好吧,它看起來像是一些列 - 最有可能是柚子 - 不喜歡。我刪除了所有列,只保留3個基本列(和int,布爾和varchar),它的工作原理。我將不得不嘗試找出它是哪一列...只要我弄清楚,我會發布...感謝大家的幫助

相關問題