.Net Core Automapper缺少类型映射配置或不支持映射。

问题描述 投票:0回答:1

我在尝试使用自动映射器,但结果出现以下错误。我试图使用自动映射器,但结果却出现了以下错误。

.Net Core Automapper missing type map configuration or unsupported mapping

我在 startup.cs 中做了如下设置。

var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });

            IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);

然后我使用profile。

  public class MappingProfile : Profile
    {

        public MappingProfile()
        {
            this.CreateMap<Geography, GeographyEntity>();
            this.CreateMap<Model1, Model2>();
        }

    }

我正在使用自动映射器,如下所示

 Model1 model = this.Mapper.Map<Model1>(Model2);

以下是型号

 public partial class Model1
    {
        public int SNo { get; set; }
        public string SarNo { get; set; }
        public string SiteName { get; set; }
        public string Client { get; set; }
        public int CId { get; set; }
        public DateTime StartDate { get; set; }
        public bool IsActive { get; set; }

        public virtual Model2 C { get; set; }
    }

public class Model2
{
    public int SNo { get; set; }

    public string SarNo { get; set; }

    public string SiteName { get; set; }

    public int CId { get; set; }

    public string Client { get; set; }

    public bool? IsActive { get; set; }

    public DateTime StartDate { get; set; }

}

我在自动映射器中得到以下错误。

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

谁能帮助我理解这个错误?任何帮助将是非常感激的。谢谢你的帮助

c# .net-core automapper
1个回答
3
投票

this.CreateMap<Model1, Model2>(); 将创建地图从 Model1Model2所以这个应该可以用。

Model2 model = this.Mapper.Map<Model2>(new Model1());

如果你想让它反过来,要么把注册名改为:

this.CreateMap<Model2, Model1>(); 

或者添加 ReverseMap 要有双向的。

this.CreateMap<Model1, Model2>().ReverseMap();
© www.soinside.com 2019 - 2024. All rights reserved.