尝试从 v4.2 升级到 AutoMapper 5.1 并发现集合在运行时未映射 - 源对象在集合中有项目,但映射的目标属性为空。
在 4.2 下,使用相同的映射配置,一切都按预期工作(除了 CreateMap() ctor 中的 MemberList.None)
我有这样的 DTO
public class GeographicEntity
{
...
}
public class County : GeographicEntity
{
...
}
public class State : GeographicEntity
{
public List<County> Counties { get; } = new List<County>();
}
像这样的视图模型
public class GeographicEntityViewModel
{
...
}
public class CountyViewModel : GeographicEntityViewModel
{
...
}
public class StateViewModel : GeographicEntityViewModel
{
public List<CountyViewModel> Counties { get; } = new List<CountyViewModel>();
}
像这样映射确认
Mapper.Initialize(configuration =>
{
configuration.CreateMap<GeographicEntity, GeographicEntityViewModel>(MemberList.None);
configuration.CreateMap<County, CountyViewModel>(MemberList.None)
.IncludeBase<GeographicEntity, GeographicEntityViewModel>();
configuration.CreateMap<State, StateViewModel>(MemberList.None)
.IncludeBase<GeographicEntity, GeographicEntityViewModel>();
});
在 Mapper.Map<> 调用之后,StateViewModel 的 Counties 集合为空(包含 0 个项目的列表),即使源对象的 .Counties 集合中有项目:
var st = new State()
... (initialize the state, including the .Counties list)
var stateViewModel = Mapper.Map<StateViewModel>(st);
任何线索将不胜感激!