c# - AutoMapper 配置验证
问题描述
我有奇怪的行为AssertConfigurationIsValid
和Map
使用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
解决方案
正如文档所说,您缺少内部类型的映射。
CreateMap<AA, BB>();
推荐阅读
- angular7 - NgbModal 一次又一次使用相同的模态后响应非常慢
- c++ - 创建多个对象并通过构造函数传递参数
- google-apps-script - 缺少链接时如何访问 Google Apps 脚本的 Cloud Platform
- javascript - 如何集成外部脚本进行反应,例如 PayPal Smart Checkout?
- c# - 在 ASP.NET Core SignalR 中管理 DbContext 生命周期
- php - 如何在 MySQL 中存储基于时间的 OTP 密钥
- sql - Explode and Count all items from 2 dates column
- javascript - 找不到嵌套命令
- azure - 在 Azure 搜索中索引视图时如何从索引中删除数据?
- reactjs - 如何使用 react-router 执行多个可选参数?