EF核心:AutoMapper ProjectTo ToListAsync引发“对象引用未设置为对象的实例”

时间:2020-04-23 06:53:49

标签: c# asp.net-core entity-framework-core automapper

我正在从ef核心上下文中查询某些实体,然后初始化dto:

IQueryable<EntityType> entities = _context
                        .Entities
                        .Where(q => true)
                        .GroupBy(q => q.SomeProperty)
                        .Select(g => new GroupedEntity { Key = g.Key, Entities = g });

                    var dto = new Dto(Whatever = "whatever");

以下可以正常工作:

dto.List = entities
                 .ProjectTo<Dto>(_mapper.ConfigurationProvider)
                 .ToList();

但是,如果我尝试使此调用异步,则会失败。

dto.List = await entities
                     .ProjectTo<Dto>(_mapper.ConfigurationProvider)
                     .ToListAsync();

例外:

消息:对象引用未设置为对象的实例。 来源:匿名托管的DynamicMethods程序集 堆栈跟踪:

在 Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor。<_ ExecuteAsync> d__8 1.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.AsyncSelectEnumerable 2.AsyncSelectEnumerator.d__3.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() 在System.Linq.AsyncEnumerable.d__6 3.MoveNext()中 D:\ a \ 1 \ s \ Ix.NET \ Source \ System.Interactive.Async \ Aggregate.cs:第128行 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)位于System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() 在 Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor。<_ ExecuteAsync> d__8 1.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务),位于System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在 Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.AsyncSelectEnumerable 2.AsyncSelectEnumerator.d__3.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务)位于System.Runtime.CompilerServices.TaskAwaiter 1.GetResult() 在 Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor 1.EnumeratorExceptionInterceptor.d__5.MoveNext() 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Runtime.CompilerServices.ConfiguredTaskAwaitable 1.ConfiguredTaskAwaiter.GetResult() 在System.Linq.AsyncEnumerable.d__6 3.MoveNext()中 D:\ a \ 1 \ s \ Ix.NET \ Source \ System.Interactive.Async \ Aggregate.cs:第128行 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务),位于System.Runtime.CompilerServices.TaskAwaiter 1.GetResult()

版本:

  • 目标框架:netcoreapp2.2
  • 实体框架核心2.2.6
  • AutoMapper 8.1.1

0 个答案:

没有答案