自动映射例外列表的列表

时间:2016-08-04 19:21:53

标签: c# automapper

我遇到了Automapper的问题。我的映射调用如下:

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

实体是IQueryable<Employee>

在我的Mapper助手课程中:

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);
            ...
        }
    }
}

在Global.asax中的AppStartup上调用一次RegisterMappings 之后我在Map操作中遇到异常:

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

映射类型时出错。

     

映射类型:   列表`1 - &gt; List`1 System.Collections.Generic.List`1 [[NAMESPACE.Employee,ASSEMBLY,   Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] - &gt;   System.Collections.Generic.List`1 [[NAMESPACE2.EmployeeDTO,ASSEMBLY2,   Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]

任何人都可以知道我做错了什么?

祝你好运

2 个答案:

答案 0 :(得分:1)

好的,这就是答案:

MapperConfiguration.AssertConfigurationIsValid()

当我调用它时,我得到了AMConfigurationException,其中包含unmaped属性,深入到另一个Employee属性中。

感谢您的建议

答案 1 :(得分:1)

在我的情况下,这是因为它试图映射应该忽略的ID