Automapper exception: “Missing type map configuration or unsupported mapping.”

和自甴很熟 提交于 2019-12-01 15:55:46

You need to add the reverse mapping also. You can do it one of two ways:

Mapper.CreateMap<AddRecordViewModel, Record>();
Mapper.CreateMap<Record, AddRecordViewModel>();

or in one go like so:

Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();

The latter is preferable if you ask me.

Don't call Mapper.CreateMap in your profile. Call base.CreateMap, and you're set:

public class RecordProfile : Profile
{
    protected override void Configure()
    {
        base.CreateMap<AddRecordViewModel, Record>().ReverseMap();
    }
}

Mapper.Initialize() should be used strictly once per solution.

If you call Initialize() somewhere later it will override all your previous mappings. Inspect your code attentively, guess, you'll find a call of this method in another place.

P.S.: That wasn't initial behavior of Automapper early, as I could see in pieces of code created 3 and more years ago on GitHub.

I had a similar problem, I forgot to register in the Global.asax

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {


        AutoMapperConfig.RegisterMappings();

    }
}

In my case the problem was that some default Automapper mappings were registered with the IoC.

builder.Register(_ => AutomapperConfiguration.Configure()).As<IMapper>().SingleInstance();

The mapping that were failing were registered in a different place, the service configuration

static void Main(string[] args)
{
    var container = ConfigureDependencies();
    AutoMapping.Configure();

The project were was getting this error was a test project where the service configuration was not executed. When debugged had the illusion that the failing mappings were registered, as could see the ones from IoC mappings.

Solution, make sure all the mappings were registered in the test solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!