user3619176
user3619176

Reputation: 81

Automapper mapping exception with lists to lists

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

Upvotes: 2

Views: 16931

Answers (3)

Chris Johnson
Chris Johnson

Reputation: 21

I was getting this error, I was able to run it in production, but the unit test failed , failed and failed.

Make sure that you include your profile if you setup your own mapper configuration. Also double check your mappings.

var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MyMissingProfile());
        });
        
        _mapper = mappingConfig.CreateMapper();

Upvotes: 0

Kylar182
Kylar182

Reputation: 119

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

Upvotes: 2

user3619176
user3619176

Reputation: 81

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

Upvotes: 4

Related Questions