2
votes

I've got problem with Automapper. My mapping call looks like:

var dataContracts = MapperManager.Mapper.Map<List<Employee>, List<EmployeeDTO>>(entities.ToList());

Entity is IQueryable<Employee>

In my Mapper helper class I Have:

public class MapperManager
{
    public static MapperConfiguration MapperConfiguration { get; set; }

    private static IMapper _mapper { get; set; }

    public static IMapper Mapper
    {
        get
        {
            if (_mapper == null) {
                _mapper = MapperConfiguration.CreateMapper();                    
            }

            return _mapper;
        }
    }

    public static void RegisterMappinngs()
    {

        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            ...
            cfg.CreateMap<Employee, EmployeeDTO>().MaxDepth(5);
            ...
        }
    }
}

RegisterMappings is called once on AppStartup in Global.asax and after that I've Exception in Map operation:

var dataContracts = MapperManager.Mapper.Map<List<Employee>, List<EmployeeDTO>>(entities.ToList());

Error mapping types.

Mapping types: List`1 -> List`1 System.Collections.Generic.List`1[[NAMESPACE.Employee, ASSEMBLY, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List`1[[NAMESPACE2.EmployeeDTO, ASSEMBLY2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Anyone can provide any idea what I'm doing wrong?

Best regards

2
Are you successfully able to map a single Employee -> EmployeeDTO? Your configuration may not be complete. What happens if you call MapperConfiguration.AssertConfigurationIsValid() ?Dean Goodman

2 Answers

2
votes

Ok, that was the answer:

MapperConfiguration.AssertConfigurationIsValid()

When I called that, I got AMConfigurationException with unmaped property, deep inside another Employee property.

Thank you for your suggestion

1
votes

In my case this was because it was attempting to map Ids that should have been ignored