我想要一个带有嵌套ProductDetailedReadDto的CategoryReadDto。
ProductDetailedReadDto:
using System.Collections.Generic;
using Categories.Dtos;
public class ProductDetailedReadDto
{
public int Id { get; set; }
public DateTime DateCreated { get; set; }
public CategoryReadDto Category { get; set; }
}CategoryReadDto:
using System;
public class CategoryReadDto
{
public int Id { get; set; }
public DateTime DateCreated { get; set; }
}ProductsProfile:
using AutoMapper;
using Products.Dtos;
using Products.Models;
public class ProductsProfile : Profile
{
public ProductsProfile()
{
// source -> target
CreateMap<Product, ProductDetailedReadDto>();
// CreateMap<Product, ProductDetailedReadDto>().ForMember(dst => dst.Category, act => act.MapFrom(src => src.Category));
}
}产品模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Categories.Models;
using ProductImages.Models;
using Reviews.Models;
public class Product
{
[Key]
public int Id { get; set; }
[Required]
public int CategoryId { get; set; }
public Category Category { get; set; }
[Required]
public DateTime DateCreated { get; set; }
[Required]
public float Price { get; set; }
public List<Review> Reviews { get; set; }
public List<ProductImage> ProductImages { get; set; }
}类别模式:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Products.Models;
public class Category
{
[Key]
public int Id { get; set; }
public DateTime DateCreated { get; set; }
[Required]
public bool Active { get; set; }
public List<Product> Products { get; set; }
}控制器代码:
[HttpGet("detailed")]
public ActionResult<IEnumerable<ProductDetailedReadDto>> GetAllProductsDetailed()
{
var productModelFromRepo = _repository.Products.GetAllProductsDetailed();
return Ok(_mapper.Map<ProductDetailedReadDto>(productModelFromRepo));
}我得到的错误是在下面,我不明白为什么。即使我用forMember取消了代码注释,并链接到ProductProfile中的类别属性。
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
Object -> ProductDetailedReadDto
System.Object -> Products.Dtos.ProductDetailedReadDto
at lambda_method240(Closure , Object , ProductDetailedReadDto , ResolutionContext )
at Eshop.Controllers.ProductsController.GetAllProductsDetailed() in C:\Users\anton\Desktop\projects\Backend\antonis-personal\Eshop\Controllers\ProductsController.cs:line 40
at lambda_method2(Closure , Object , Object[] )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)发布于 2022-04-20 06:48:12
正如卢西恩·巴戈阿努指出的那样,我应该在控制器中加上这一点。
Map<<IEnumerable<ProductDetailedReadDto>>https://stackoverflow.com/questions/71930545
复制相似问题