0

我在 web api 中有 get 方法:

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 并返回结果。我只想使用AutoMapper.QueryableExtensions并选择必填字段。

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”:“缺少从 CategoriesAndBrandsServices.Models.Category 到 System.Collections.Generic.List 1[CategoriesAndBrandsServices.Dtos.CategoryDto]. Create using Mapper.CreateMap<Category, List1> 的映射。”、“exceptionType”:“System.InvalidOperationException”、“stackTrace”:“在 AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression( ExpressionRequest 请求,Expression instanceParameter,IDictionary 2 typePairCount)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary2 typePairCount)\r\n 在 AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request)\r\n 在 AutoMapper.LockingConcurrentDictionary 2.<>c__DisplayClass2_1.<.ctor>b__1()\r\n at System.Lazy1.CreateValue()\r\n 在 System.Lazy 1.LazyInitValue()\r\n at System.Lazy1.get_Value ()\r\n 在 AutoMapper.LockingConcurrentDictionary 2.GetOrAdd(TKey key)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(Type sourceType, Type destinationType, IDictionary2 参数,MemberInfo[] membersToExpand)\r\n 在 AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](IDictionary2 parameters, IEnumerable1 memberPathsToExpand)\r\n 在 AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](对象参数,表达式1[] membersToExpand)\r\n at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression1[] membersToExpand)\r\n 在 AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable 源,表达式1[] 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 在 System.Threading.Tasks.TaskHelpersExtensions.d__3 1.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 在 System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- 结束从先前引发异常的位置的堆栈跟踪 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r\n 在 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 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"

4

1 回答 1

1

我假设该项目是一对一的,并且您尝试从类别转换到类别 dtos 列表所以尝试用这个替换项目。

.ProjectTo<CategoryDto>()

PS。从异常“缺少从 CategoriesAndBrandsServices.Models.Category 到 System.Collections.Generic.List1[CategoriesAndBrandsServices.Dtos.CategoryDto] 的映射。使用 Mapper.CreateMap 创建。”

于 2017-01-09T05:43:36.870 回答