首页 > 解决方案 > AutoMapper 配置验证

问题描述

我有奇怪的行为AssertConfigurationIsValidMap使用AutoMapper 10.1.1

源数据:

private class A
{
    public AA Field { get; set; }
}

private class AA
{
    public string InnerField { get; set; }
}

private class B
{
    public BB Field { get; set; }
}

private class BB
{
    public string InnerField { get; set; }
}

测试方法,将成功执行:

public void Test()
{
    var mapperConfig = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<A, B>();
    });
    var mapper = new Mapper(mapperConfig);
    var a = new A();

    mapper.Map<B>(a);
}

但是如果我在映射之前添加调用mapperConfig.AssertConfigurationIsValid()它会抛出异常

AutoMapper.AutoMapperConfigurationException : The following member on AutoMapperTestsSO+B cannot be map...

AutoMapper.AutoMapperConfigurationException : The following member on AutoMapperTestsSO+B cannot be mapped: 
    Field 
Add a custom mapping expression, ignore, add a custom resolver, or modify the destination type AutoMapperTestsSO+B.

升级版:

主要问题:我不清楚为什么Map在没有第一次调用的情况下调用AssertConfigurationIsValid有效并且不会引发异常。如果使用相同的映射配置执行相同的检查并返回相同的结果,AssertConfigurationIsValid这将是一个合乎逻辑的行为Map

标签: c#automapper

解决方案


正如文档所说,您缺少内部类型的映射。

CreateMap<AA, BB>();

推荐阅读