我在AutoMapper文档中读到UseAsDataSource应该与ProjectTo做同样的工作。但是,我确实有一个奇怪的问题:
此代码有效:
var _query = this.IUnitOfWork.IDataCrmRepository.contacts
.Where(w => w.org_id == config_org_id)
.UseAsDataSource(this.IMapper.ConfigurationProvider)
.For<DTO.contact>()
;
// nov order by optional navigation property (address) which may be null
_query = _query.OrderBy(o => o.address.city);
return _query.ToArray();
此代码不起作用:
var _query = this.IUnitOfWork.IRepository.contacts
.Where(w => w.org_id == config_org_id)
.ProjectTo<DTO.contact>(this.IMapper.ConfigurationProvider)
;
// nov order by optional navigation property (address) which may be null
_query = _query.OrderBy(o => o.address.city);
return _query.ToArray();
我收到此错误:
Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLIBS29CUR23", Request id "0HLIBS29CUR23:00000006": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , TransparentIdentifier`2 )
at System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.ComputeMap(TElement[] elements, Int32 count)
at System.Linq.EnumerableSorter`1.ElementAt(TElement[] elements, Int32 count, Int32 idx)
at System.Linq.OrderedEnumerable`1.GetEnumerator(Int32 minIdx, Int32 maxIdx)+MoveNext()
at System.Linq.Enumerable.SelectIPartitionIterator`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
那是区别吗?
好的,我不必了解所有内容,让我们呆在一起UseAsDataSource
。但是,当我尝试实现异步/等待时,到return await _query.ToArrayAsync();
不能与UseAsDataSource
一起使用时,又出现了另一个错误:
Connection id "0HLIBS9MUOGQR", Request id "0HLIBS9MUOGQR:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: The provider for the source IQueryable doesn't implement IAsyncQueryProvider. Only providers that implement IEntityQueryProvider can be used for Entity Framework asynchronous operations.
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
为了使事情变得更加复杂,当我删除.OrderBy调用时,ProjectTo可与async / await一起很好地工作。
有什么主意吗?