首页 > 解决方案 > .Net Core Automapper 缺少类型映射配置或不支持的映射

问题描述

网络核心应用。我正在尝试使用 Auto mapper,但导致以下错误。

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

然后我正在使用配置文件。

  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:缺少类型映射配置或不支持的映射。

有人可以帮我理解这个错误吗?任何帮助将不胜感激。谢谢

标签: c#.net-coreautomapper

解决方案


this.CreateMap<Model1, Model2>();将从Model1to创建地图Model2,所以这应该工作:

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

如果您想要反之亦然,请将注册更改为:

this.CreateMap<Model2, Model1>(); 

或添加ReverseMap双向:

this.CreateMap<Model1, Model2>().ReverseMap();

推荐阅读