全部。例如,我們有這樣一個簡單的類未映射嵌套集合內的Automapper無法正常工作
public class SimpleModel
{
public int PropertyId { get; set; }
public ICollection<SimpleModelCollectionItem> SimpleCollection { get; set; }
}
public class SimpleModelCollectionItem
{
public int PropertyId { get; set; }
}
public class SimpleEntity
{
public int Id { get; set; }
public int PropertyId { get; set; }
public virtual ICollection<SimpleEntityCollectionItem> SimpleCollection { get; set; }
}
public class SimpleEntityCollectionItem
{
public int Id { get; set; }
public int PropertyId { get; set; }
}
,我們有一些配置代碼
AutoMapper.Mapper.CreateMap<SimpleModel, SimpleEntity>()
.ForMember(dest => dest.Id, src => src.Ignore())
.ForMember(dest => dest.SimpleCollection, src => src.UseDestinationValue());
AutoMapper.Mapper.CreateMap<SimpleModelCollectionItem, SimpleEntityCollectionItem>()
.ForMember(dest => dest.Id, src => src.Ignore());
AutoMapper.Mapper.AssertConfigurationIsValid();
和測試數據初始化
var model = new SimpleModel
{
PropertyId = 2,
SimpleCollection =
new List<SimpleModelCollectionItem>
{
new SimpleModelCollectionItem
{
PropertyId = 7
}
}
};
var entity = new SimpleEntity
{
Id = 1,
PropertyId = 3,
SimpleCollection =
new List<SimpleEntityCollectionItem>
{
new SimpleEntityCollectionItem
{
Id = 5,
PropertyId = 4
}
}
};
AutoMapper.Mapper.Map(model, entity);
,我希望看到
Console.WriteLine(entity.Id); // 1
Console.WriteLine(entity.PropertyId); // 2
Console.WriteLine(entity.SimpleCollection.First().Id); // 5 but was 0
Console.WriteLine(entity.SimpleCollection.First().PropertyId); // 7
是否有可能內部收集的Id等於5,因爲它最初是?