简体   繁体   English

带有列表到列表的自动映射器映射异常

[英]Automapper mapping exception with lists to lists

I've got problem with Automapper.我对 Automapper 有疑问。 My mapping call looks like:我的映射调用如下所示:

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

Entity is IQueryable<Employee>实体是IQueryable<Employee>

In my Mapper helper class I Have:在我的 Mapper 助手 class 中,我有:

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: RegisterMappings 在 Global.asax 中的 AppStartup 上被调用一次,之后我在 Map 操作中出现异常:

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]]映射类型: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最好的祝福

Ok, that was the answer:好的,这就是答案:

MapperConfiguration.AssertConfigurationIsValid() MapperConfiguration.AssertConfigurationIsValid()

When I called that, I got AMConfigurationException with unmaped property, deep inside another Employee property.当我调用它时,我得到了 AMConfigurationException 与未映射的属性,在另一个 Employee 属性的深处。

Thank you for your suggestion谢谢你的建议

就我而言,这是因为它试图映射本应被忽略的 ID

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM