0
votes

For my MVC project, I upgraded my nuget packages and got latest version of AutoMapper from https://www.nuget.org/packages/AutoMapper/

It says IList is supported as mapping source; https://github.com/AutoMapper/AutoMapper/wiki/Lists-and-arrays

It was working with older version and I only updated my configuration section.

Configuration is as below;

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {           
        AutoMapperConfig.RegisterMappings();            
    }
}

public static void RegisterMappings()
{
    Mapper.Initialize(cfg =>
    {
       cfg.CreateMap<RssNewDto, RssNewViewModel>();    
    });   
}   

// where I am trying to resolve
[HttpGet]
public IList<RssNewViewModel> ReadList()
{
     // EXCEPTION
    IList<RssNewViewModel> items2 = AutoMapper.Mapper.Map<IList<RssNewDto>, IList<RssNewViewModel>>(items);
    return items2;
}

ERROR: AutoMapper.AutoMapperMappingException occurred
HResult=-2146233088 Message=Error mapping types. InnerException: HResult=-2146233088 Message=Missing type map configuration or unsupported mapping.

Am I missing something on configuration?

1
What's the rest of the exception message? It should include the missing type information. - Jimmy Bogard
@JimmyBogard after I call MapperConfiguration.AssertConfigurationIsValid method it showed me all missing config errors clearly. I believe this one should be called internally on after Initialize method. Since people may forgotten to call this method may have invalid configured Mapper and it will bite them on runtime. - Teoman shipahi
Oh yeah, I see a lot of people do that. I'm not sure about doing it in production, but I think I'm in the minority there. - Jimmy Bogard

1 Answers

0
votes

Your RegisterMappings method is only creating a map from the RssNewDto to the RssNewViewModel, not from an IList<RssNewDto> to IList<RssNewViewModel>.

You could do this items.Select(item => AutoMapper.Mapper.Map<RssNewViewModel>(item)).ToList();