AutoMapper - 试图将我的对象属性展平为字符串但出现错误

Posted

技术标签:

【中文标题】AutoMapper - 试图将我的对象属性展平为字符串但出现错误【英文标题】:AutoMapper - Trying to flatten my object property to string but getting an error 【发布时间】:2021-12-20 22:13:59 【问题描述】:

我正在尝试将我的 ProductDto 映射到 Product 类。将 ProductDto 类中的字符串属性映射到 Product 类中的 Brand 属性时出现错误

AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
ProductDto -> Product
SilksyAPI.Dto.ProductDto -> SilksyAPI.Entities.Product

Type Map configuration:
ProductDto -> Product
SilksyAPI.Dto.ProductDto -> SilksyAPI.Entities.Product

Destination Member:
Brand

 ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
String -> Brand
System.String -> SilksyAPI.Entities.Brand

这是我的映射配置文件:

 CreateMap<Product, ProductDto>()
                .ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
                .ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)));

            CreateMap<ProductDto, Product>()
                .ForMember(dest => dest.Brand, opt => opt.MapFrom(src => src.Name));

类:


public class Product
    
        public int Id  get; set; 
        public string Name  get; set; 
        public string Description  get; set; 
        public decimal Price  get; set; 

        public int BrandId  get; set; 
        public Brand Brand  get; set; 

        public ICollection<ProductCategory> ProductCategories  get; set; 
    
 public class Brand
    
        public int Id  get; set; 

        [Required]
        [MaxLength(100)]
        public string Name  get; set; 
    

Dto类

 public class ProductDto
    
        public int Id  get; set; 
        public string Name  get; set; 
        public string Description  get; set; 
        public decimal Price  get; set; 
        public string Brand  get; set; 
        public List<string> Categories  get; set; 

    

我尝试了不同的映射配置文件,但我无法从 ProductDto 映射到产品,但可以毫无问题地从 Product 映射到 ProductDto。

这是我尝试过的其他几件事

       CreateMap<Brand, string>()
             .IncludeMembers(b => b.Name);


CreateMap<Product, ProductDto>()
                .ForMember(dest => dest.Brand, opt => opt.MapFrom(p => p.Brand.Name))
                .ForMember(dest => dest.Categories, opt => opt.MapFrom(p => p.ProductCategories.Select(pc => pc.Category.Name)))
                .ReverseMap();


Mapper.CreateMap<Brand, string>().ConvertUsing(source => source.Name ?? string.Empty);

https://docs.automapper.org/en/stable/Flattening.html

有人可以帮忙

【问题讨论】:

【参考方案1】:

从您的第一个映射配置文件中,当从ProductDto 映射到Product 时,对于Brand 属性,您需要创建一个新的Brand 实例,如下所示:

cfg.CreateMap<ProductDto, Product>()
   .ForMember(dest => dest.Brand, opt => opt.MapFrom(src => new Brand  Name = src.Name ));

Sample Program


对于.IncludeMembers()

我认为它不适合您的方案,因为您正在从 object 映射到 string 而不是 objectobject


对于.ConvertUsing()

您需要将以下代码添加到 Brand 属性从 stringBrand 的映射中。

cfg.CreateMap<string, Brand>()
   .ConvertUsing(source => new Brand  Name = source );

Sample program (.ConvertUsing())

【讨论】:

感谢您的帮助!

以上是关于AutoMapper - 试图将我的对象属性展平为字符串但出现错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 AutoMapper 展平嵌套对象的更好方法?

展平嵌套对象以将其属性映射到目标对象

如何调试到 AutoMapper 代码?

将展平对象的属性映射为集合

“缺少从 System.Char 到 System.String 的映射”AutoMapper 错误

C#console app与DB first Entity Framework和AutoMapper,无法将Model转换为DbModel