嘗試從v4.2升級到AutoMapper 5.1,並發現集合在運行時未映射 - 源對象在集合中有項目,但映射的目標屬性爲空。嵌套集合在AutoMapper 5.1中無效
在4.2,一切工作完全一樣使用相同的映射配置預期(保存在CreateMap()構造函數的MemberList.None)
我的DTO像這樣
public class GeographicEntity
{
...
}
public class County : GeographicEntity
{
...
}
public class State : GeographicEntity
{
public List<County> Counties { get; } = new List<County>();
}
而且喜歡的ViewModels所以
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的縣集合爲空(0項的列表),即使源對象有其.Counties集合中的項目:
var st = new State()
... (initialize the state, including the .Counties list)
var stateViewModel = Mapper.Map<StateViewModel>(st);
任何線索將不勝感激!