我在 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>."的映射, "exceptionType": "System.InvalidOperationException", "stackTrace": " at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, Expression instanceParameter, IDictionary2 typePairCount)rn at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary
2 typePairCount)\r at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request)\r at AutoMapper.LockingConcurrentDictionary2.<>c__DisplayClass2_1.<.ctor>b__1()rn at System.Lazy
1.CreateValue()\r at System.Lazy1.LazyInitValue()rn at System.Lazy
1.get_Value()\r at AutoMapper.LockingConcurrentDictionary2.GetOrAdd(TKey key)rn at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(Type sourceType, Type destinationType, IDictionary
2 个参数, MemberInfo[]membersToExpand)\r at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](IDictionary2 parameters, IEnumerable
1 memberPathsToExpand)\r at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression1[] membersToExpand)rn at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression
1[] membersToExpand)\r at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, Expression1[] membersToExpand)rn at CategoriesAndBrandsServices.Controllers.CategoriesController.<Get>d__0.MoveNext() in c:\OnlineShoppingWebsite\Services\CategoriesAndBrandsServices\CategoriesAndBrandsServices\Controllers\CategoriesController.cs:line 24rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\r at System.Threading.Tasks.TaskHelpersExtensions.d__31.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\r atSystem.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r------ 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()rn at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()rn--- End of stack trace from previous location where exception was thrown ---rn at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)rn at System.Runtime.CompilerServices.TaskAwaiter
1.GetResult()\r at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1。移动下一个()">
我假设项目是一对一的,并且您尝试从一个类别转换为类别列表 dtos 因此,请尝试与此进行项目讨论。
.ProjectTo<CategoryDto>()
附言。 从异常"缺少来自 CategoriesAndBrandsServices.Models.Category to System.Collections.Generic.List1[CategoriesAndBrandsServices.Dtos.CategoryDto] 的映射。使用 Mapper.CreateMap 创建。