我在 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, List
1> 的映射。”、“exceptionType”:“System.InvalidOperationException”、“stackTrace”:“在 AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression( ExpressionRequest 请求,Expression instanceParameter,IDictionary2 typePairCount)\r\n at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary
2 typePairCount)\r\n 在 AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request)\r\n 在 AutoMapper.LockingConcurrentDictionary2.<>c__DisplayClass2_1.<.ctor>b__1()\r\n at System.Lazy
1.CreateValue()\r\n 在 System.Lazy1.LazyInitValue()\r\n at System.Lazy
1.get_Value ()\r\n 在 AutoMapper.LockingConcurrentDictionary2.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](IDictionary2 parameters, IEnumerable
1 memberPathsToExpand)\r\n 在 AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](对象参数,表达式1[] membersToExpand)\r\n at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression
1[] 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.TaskAwaiter
1。 GetResult()\r\n 在 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.TaskAwaiter
1.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.TaskAwaiter
1.GetResult()\r\n 在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"