Random “Missing type map configuration or unsupported mapping.” Error in Automapper

前端 未结 2 1383
梦如初夏
梦如初夏 2021-01-26 09:47

Please see this post for the solution.

Ok, I finally figured it out. The: AppDomain.CurrentDomain.GetAssemblies() piece of my code sometimes does not

相关标签:
2条回答
  • 2021-01-26 10:26

    For me this error had to do with where I put my CreateMap<>() call. I had put it in the static initializer for my DTO. When I moved the CreateMap<>() call to somewhere less cute, everything worked fine.

    0 讨论(0)
  • 2021-01-26 10:29

    If you use Mapper.AssertConfigurationIsValid(); you would get bit more detailed info:

    Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

    In any case, you have to have mapped all properties of destination model. You were missing CityDTO and Id. Here:

    Mapper.CreateMap<City, CityDTO>();
    
    Mapper.CreateMap<Country, CountryDTO>()
        .ForMember(dto => dto.Id, options => options.Ignore())
        .ForMember(dto => dto.Longtitude, mc => mc.MapFrom(e => e.CountryCoordinate.Longtitude))
        .ForMember(dto => dto.Lattitude, mc => mc.MapFrom(e => e.CountryCoordinate.Lattitude));
    

    Maybe you would need some additional mapping on City-CityDTO, as you did not specify them.

    0 讨论(0)
提交回复
热议问题