繁体   English   中英

C# 带有嵌套 dto 的 Automapper DTO

[英]C# Automapper DTO with nested dto

我想要一个带有嵌套 CategoryReadDto 的 ProductDetailedReadDto。

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; }
  }

类别ReadDto:


using System;


  public class CategoryReadDto
  {
    public int Id { get; set; }
    public DateTime DateCreated { get; set; }
  }

产品简介:

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));
    }
  }

产品 Model:

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; }
  }

类别 Model:

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; }


  }

Controller 代码:

  [HttpGet("detailed")]
    public ActionResult<IEnumerable<ProductDetailedReadDto>> GetAllProductsDetailed()
    {
      var productModelFromRepo = _repository.Products.GetAllProductsDetailed();
      return Ok(_mapper.Map<ProductDetailedReadDto>(productModelFromRepo));
    }

我得到的错误是下面这个,我不明白为什么。 即使我使用 forMember 取消注释代码并链接 ProductProfile 中的 Category 属性。

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)

正如 Lucian Bargaoanu 指出的那样,我应该在 controller 中添加这个

Map<<IEnumerable<ProductDetailedReadDto>>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM