AutoMapper投影错误

时间:2017-01-09 03:57:23

标签: c# entity-framework-6 automapper-5

我在web 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并返回结果。我想使用 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, List 1&gt;。”,     “exceptionType”:“System.InvalidOperationException”,     “stackTrace”:“AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest请求,Expression instanceParameter,IDictionary 2 typePairCount)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary 2 typePairCount)\ r \ n在AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request)\ r \ n at AutoMapper.LockingConcurrentDictionary 2.<>c__DisplayClass2_1.<.ctor>b__1()\r\n at System.Lazy 1.CreateValue()\ r \ n在System.Lazy 1.LazyInitValue()\r\n at System.Lazy 1.get_Value()\ r \ n在AutoMapper.LockingConcurrentDictionary 2.GetOrAdd(TKey key)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(Type sourceType, Type destinationType, IDictionary 2个参数,MemberInfo [] membersToExpand)\ r \ n在AutoMapper.QueryableExtensions.ProjectionExpression.To [TResult](IDictionary 2 parameters, IEnumerable 1 memberPathsToExpand)\ r \ n在AutoMapper.QueryableExtensions.ProjectionExpression.To [TResult](对象参数,表达式{{1在AutoMapper.QueryableExtensions.Extensions.ProjectTo [TDestination](IQueryable source,Expression 1[] membersToExpand)\r\n at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression 1.GetResult()\ r \ n在System.Threading.Tasks.TaskHelpersExtensions中的1 [] membersToExpand)\ r \ n。 d__3 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.TaskAwaiter 1.GetResult()\ r \ n在System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.Mov eNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在System.Runtime上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n \ n \ n System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext上的.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter 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.TaskAwaiter 1.GetResult()\ r \ n ()“

1 个答案:

答案 0 :(得分:1)

我假设项目为一对一,并且您尝试从类别转换为类别列表dtos 所以尝试用这个来重新计划项目。

.ProjectTo<CategoryDto>()

PS。 从例外&#34;从CategoriesAndBrandsServices.Models.Category到System.Collections.Generic.List1 [CategoriesAndBrandsServices.Dtos.CategoryDto]缺少地图。使用Mapper.CreateMap创建。&#34;