【问题标题】:EF Core: An exception occurred in the database while iterating the results of a queryEF Core:迭代查询结果时数据库中发生异常
【发布时间】:2020-11-04 00:52:19
【问题描述】:

我从托管在 Azure 中的 Web 应用程序中收到此错误。它运行了几天,然后突然停止工作。

连接可能有问题吗?哪个对象引用没有实例?

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

如果我将存储库方法转换为同步并在控制器中保持异步,会有帮助吗?

【问题讨论】:

  • 你能提供你的连接字符串吗?也许您需要在其中添加“MultipleActiveResultSets=true”
  • 这是错误的。我将其设置为 true 并试一试。谢谢。
  • 将其设置为 True 并让它运行后,它又可以工作了。几天后,我现在收到另一个错误:“连接不支持 MultipleActiveResultSets。”我不知道为什么当它工作了几次时我会得到它。

标签: asp.net-web-api azure-sql-database azure-web-app-service entity-framework-core


【解决方案1】:

它运行了几天,然后突然停止工作。

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,您可以检查它。

【讨论】:

  • 我会尝试按照 hugorgor 的建议将 MARS 设置为 true。我不能为每个请求做一个实例,因为我希望注入 DbContext。是否可以按请求执行但仍从存储库外部的某处注入?
  • 您使用的似乎是依赖注入框架,请尝试使用DI容器注入具有PerWebRequest生命周期的DbContext实例。
  • Transient 不是默认生命周期吗?我认为这相当于每个网络请求。
  • Isn't Transient the default lifetime? That's equivalent to per web request Scoped lifetime services 将在每个请求中创建一次。
  • 我刚刚查过了。 “AddDbContext 的默认值为 Scoped,这意味着在 Web 请求期间所有类都将访问相同的 SqlContext。”应该没问题吧?
猜你喜欢
  • 2021-12-11
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 2019-02-09
  • 2021-11-18
相关资源
最近更新 更多