AutoMapper 投影错误

AutoMapper projection error

我在网络中有get方法api:

public async Task<IHttpActionResult> Get()
{
   var categories = await _context.Categories.Include(x => x.SubCategories)
                                  .Where(c => c.IsActive)
                                  .ToListAsync();
   var outPut = AutoMapper.Mapper.Map<List<CategoryDto>>(categories);
   return Ok(outPut);
}

此方法按预期工作。但我不想从数据库中检索所有记录然后映射到 DTO 和 return result.I 想使用 AutoMapper.QueryableExtensions 和 select只有必填字段。

var categories = await _context.Categories.Include(x => x.SubCategories)
                                          .Where(c => c.IsActive)
                                          .ProjectTo<List<CategoryDto>>()
                                          .ToListAsync();

在映射配置中,我将映射定义为:

public static void Config()
{
    AutoMapper.Mapper.Initialize(config =>
    {
        config.CreateMap<SubCategory, SubCategoryDto>();
        config.CreateMap<Category, CategoryDto>().ForMember(
                    des=>des.SubCategoriesCount,
                    opt=>opt.MapFrom(src=>src.SubCategories.Count));
    });

}

虽然 运行 我遇到以下异常

"exceptionMessage": "Missing map from CategoriesAndBrandsServices.Models.Category to System.Collections.Generic.List1[CategoriesAndBrandsServices.Dtos.CategoryDto]. Create using Mapper.CreateMap<Category, List1>.", "exceptionType": "System.InvalidOperationException", "stackTrace": " at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, Expression instanceParameter, IDictionary2 typePairCount)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary2 typePairCount)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request)\r\n at AutoMapper.LockingConcurrentDictionary2.<>c__DisplayClass2_1.<.ctor>b__1()\r\n at System.Lazy1.CreateValue()\r\n at System.Lazy1.LazyInitValue()\r\n at System.Lazy1.get_Value()\r\n at AutoMapper.LockingConcurrentDictionary2.GetOrAdd(TKey key)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(Type sourceType, Type destinationType, IDictionary2 parameters, MemberInfo[] membersToExpand)\r\n at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](IDictionary2 parameters, IEnumerable1 memberPathsToExpand)\r\n at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression1[] membersToExpand)\r\n at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression1[] membersToExpand)\r\n at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, Expression1[] membersToExpand)\r\n at CategoriesAndBrandsServices.Controllers.CategoriesController.<Get>d__0.MoveNext() in c:\OnlineShoppingWebsite\Services\CategoriesAndBrandsServices\CategoriesAndBrandsServices\Controllers\CategoriesController.cs:line 24\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Threading.Tasks.TaskHelpersExtensions.d__31.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"

我假设项目 to 是一对一的,你尝试从类别转换到类别 dto 列表 所以尝试用这个替换项目。

.ProjectTo<CategoryDto>()

PS。 来自异常 "Missing map from CategoriesAndBrandsServices.Models.Category to System.Collections.Generic.List1[CategoriesAndBrandsServices.Dtos.CategoryDto]. Create using Mapper.CreateMap."