【问题标题】:Asp.net Error When trying to map two classes尝试映射两个类时出现 Asp.net 错误
【发布时间】:2022-08-14 10:23:40
【问题描述】:

我正在尝试使用 AutoMapper 将 DTO 映射到 Entity 类,但我不断收到错误消息。这是 DTO 类

    public class Product
        {
     public string ID { get; set; }
            public string SKU { get; set; }
            public string Name { get; set; }
    public PriceTiers PriceTiers { get; set; }
}

这是实体

public partial class Product
    {
        public Product()
        {
            PriceTiers = new List<PriceTiers>();
        }

        [Key]
        public string ID { get; set; }
        public string SKU { get; set; }
        public string Name { get; set; }
 public virtual ICollection<PriceTiers> PriceTiers { get; set; }
    }

为什么我不断收到以下错误

{\"Missing type map configuration or unsupported mapping.\\r\\n\\r\\nMapping types:\\r\\nPriceTiers -> ICollection`1\\r\\nWeb.Areas.DEAR.DTOs.PriceTiers -> 
System.Collections.Generic.ICollection`1[[Web.Areas.DEAR.Data.PriceTiers, Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]\\r\\n\\r\\n
Destination Member:\\r\\nPriceTiers\\r\\n\"}

这就是我在 Profile 类中的内容

AllowNullCollections = true;
            CreateMap<DTOs.Product, Data.Product>();
            CreateMap<DTOs.PriceTiers, Data.PriceTiers>();

这就是我用来映射类的

var products = _mapper.Map<IEnumerable<Product>>(result.Products);

这就是 program.cs 中的内容

builder.Services.AddAutoMapper(typeof(AutoMapperProfiles).Assembly);

    标签: asp.net .net automapper


    【解决方案1】:

    您是否在配置后添加了“CreateMapper()”方法? 尝试类似的东西。

    public class MappingProfile : Profile
    {
        public MappingProfile {
            AllowNullCollections = true;
            CreateMap<DTOs.Product, Data.Product>();
            CreateMap<DTOs.PriceTiers, Data.PriceTiers>();
        }
    }
    

    之后,在您的容器服务上,注入此依赖项:

    var mappingConfig = new MapperConfiguration(cfg =>
    {
        cfg.AddProfile(new MappingProfile());
    });
    
    IMapper mapper = mappingConfig.CreateMapper();
    builder.Services.AddSingleton(mapper);
    

    【讨论】:

    • 我想我在 builder.Services.AddAutoMapper(typeof(AutoMapperProfiles).Assembly);
    【解决方案2】:

    异常消息很清楚,AutoMapper 不知道如何将数据从DTOs.PriceTiers 映射到ICollection&lt;Data.PriceTiers&gt;


    解决方案 1:从 DTOs.PriceTiers 映射到 ICollection&lt;Data.PriceTiers&gt;

    我相信Custom Type Converters 是您所需要的。

    1. 创建自定义类型转换器。
      public class ICollectionDataPriceTiersTypeConverter : ITypeConverter<DTOs.PriceTiers, ICollection<Data.PriceTiers>>
      {
          public ICollection<Data.PriceTiers> Convert(DTOs.PriceTiers src, ICollection<Data.PriceTiers> dest, ResolutionContext context)
          {
              if (src == null)
                  return default;
          
              var singleDest = context.Mapper.Map<Data.PriceTiers>(src);
              
              return new List<Data.PriceTiers>
              {
                  singleDest
              };
          }
      }
      
      1. 添加到映射配置文件。
      CreateMap<DTOs.PriceTiers, ICollection<Data.PriceTiers>>()
          .ConvertUsing<ICollectionDataPriceTiersTypeConverter>();
      

      Demo @ .NET Fiddle


      解决方案 2:从 ICollection&lt;DTOs.PriceTiers&gt; 映射到 ICollection&lt;Data.PriceTiers&gt;

      如果DTOs.Product 中的PriceTiers 支持多个项目并与多对多(到ICollection&lt;Data.ProductTiers&gt;),然后考虑将属性修改为ICollection&lt;DTOs.PriceTiers&gt; 类型。

      namespace DTOs
      {
          public class Product
          {
              ...
              public ICollection<PriceTiers> PriceTiers { get; set; }
          }
      }
      

    【讨论】:

      猜你喜欢
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多