1

我從Azure託管的我的Web應用程序中收到此錯誤。它運行好幾天,然後突然停止工作。EF Core:迭代查詢結果時數據庫中發生異常

連接有問題嗎?哪個對象引用沒有實例?

YYYY-MM-DD hh:mm:ss [Error] An exception occurred in the database while iterating the results of a query. 
System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<BufferAllAsync>d__12.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.EntityFrameworkCore.Query.RelationalQueryContext.<RegisterValueBufferCursorAsync>d__14.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<BufferlessMoveNext>d__9.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.<MoveNext>d__8.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 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.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.<_FirstOrDefault>d__82`1.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.TaskResultAsyncEnumerable`1.Enumerator.<MoveNext>d__3.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 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.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext() 
--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at MyProject.Data.SectionRepository.<GetByTypePathAndAlias>d__6.MoveNext() in /Users/webaccount/myproject/src/MyProject.Data/Repositories/SectionRepository.cs:line 34 

我的存儲庫或實體代碼沒有什麼特別之處。

public async Task<Section> GetByTypeId(string typeId) 
{ 
    var sections = from s in this.DbContext.Sections 
     where s.TypeId == typeId 
     select s; 

    return await sections.FirstOrDefaultAsync(); // <-- LINE 34 
} 

這是我的實體。

public class Section 
{ 
     [Key] 
     public int Id { get; set; } 
     public DateTimeOffset CreatedDate { get; set; } 
     public string TypeId { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
} 

這是否與此有關? https://github.com/aspnet/EntityFrameworkCore/issues/8026

如果我將存儲庫方法轉換爲在控制器上同步並保持異步,它會有幫助嗎?

+1

你能提供你的連接字符串嗎?也許你需要在其中添加「MultipleActiveResultSets = true」 – hugoterelle

+0

這是錯誤的。我會設置它爲真,並嘗試一下。謝謝。 –

+0

將其設置爲True並讓它運行後,再次運行。幾天後,我現在又得到一個錯誤:「連接不支持MultipleActiveResultSets。」我不確定爲什麼當它工作幾次時我就能得到它。 –

回答

0

It runs ok for a few days and then suddenly stops working.

Object reference not set to an instance of an object

首先,你可以嘗試remote debug your web app並檢查this.DbContext。其次,您可以爲每個請求創建/使用一個上下文實例,並且this artciel shows some general guidelines when deciding on the lifetime of the context,您可以檢查它。

+0

我會嘗試按照hugorgor的建議設置MARS爲true。我不能爲每個請求做一個實例,因爲我想要注入DbContext。是否可以按請求執行,但是仍然從庫之外的某處注入? –

+0

看來您使用的是依賴注入框架,請嘗試使用DI容器注入具有PerWebRequest生命週期的DbContext實例。 –

+0

不是瞬態的默認生命週期?這相當於每個Web請求,我想。 –

相關問題