.NET Core AutoMapper Использовать AsDataSource или ProjectTo - PullRequest
0 голосов
/ 16 ноября 2018

Я прочитал в AutoMapper document, что 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.Однако, когда я пытаюсь реализовать async / await, чтобы 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.

Есть идеи?

...