Reputation: 10710
I have scoured the web and I can't figure out why in Automapper 6.2.1 I am having these issues. I have looked through many tutorials, guides and answers and come up with nothing.
I am getting this error:
Unmapped members were found. Review the types and members below.
Unmapped properties:
Title
Subtitle
ProductType
Language
Description
However I have created mappings for these properties. I'm not sure why I am having this issue.
I have created an AutoMapperConfig
class that is setup like this:
public class AutoMapperConfig
{
public static void Initialize()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Shop, SearchViewModel>()
.ForMember(x => x.Title, opts => opts.MapFrom(x => x.TITLE))
.ForMember(x => x.Subtitle, opts => opts.MapFrom(x => x.SUB_TITLE))
.ForMember(x => x.ProductType, opts => opts.MapFrom(x => x.PRODUCT_TYPE))
.ForMember(x => x.Language, opts => opts.MapFrom(x => x.PRODUCT_LANGUAGE))
.ForMember(x => x.Description, opts => opts.MapFrom(x => x.BRIEF_DESC));
});
}
Then in my Global.asax
in the Application_Start()
method I have this line:
AutoMapperConfig.Initialize();
I am trying to use this in my controller like this:
var searchResults = Mapper.Map<List<Shop>, SearchViewModel>(shopList);
After I hit that line it throws that exception. I'm not sure why since I have created maps for all of those properties.
Upvotes: 2
Views: 4780
Reputation:
The datatype of searchResults
is wrong.
To avoid this mistake in future, declare your variables with an explicit datatype. The following would never have compiled:
List<SearchViewModel> searchResults = Mapper.Map<List<Shop>, SearchViewModel>(shopList);
Try this instead:
List<SearchViewModel> searchResults = Mapper.Map<List<Shop>, List<SearchViewModel>>(shopList);
Upvotes: 2
Reputation: 4812
This is how I have the AutoMapper
set up in one of my projects (tailored to your example). I am using a static class and a static IMapper
property inside the same class.
public static class AutoMapperConfig
{
public static IMapper EntityMapper { get; set; }
static EntityMap()
{
EntityMapper = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Shop, SearchViewModel>()
.ForMember(x => x.Title, opts => opts.MapFrom(x => x.TITLE))
.ForMember(x => x.Subtitle, opts => opts.MapFrom(x => x.SUB_TITLE))
.ForMember(x => x.ProductType, opts => opts.MapFrom(x => x.PRODUCT_TYPE))
.ForMember(x => x.Language, opts => opts.MapFrom(x => x.PRODUCT_LANGUAGE))
.ForMember(x => x.Description, opts => opts.MapFrom(x => x.BRIEF_DESC));
});
}
}
You can call it like this:
EntityMap.EntityMapper.Map<List<Shop>, SearchViewModel>(shopList);
Upvotes: 2