AutoMapper: Why is UseValue only executed once

…衆ロ難τιáo~ 提交于 2019-12-04 23:15:33

Try the MapFrom option. It provides a delegate that will be called each time a map happens. From a quick DateTime test and my command window this seems to work.

Something like:

public class Foo {
    public DateTime bar { get; set; }
}

public class Foo1
{
    public DateTime bar1 { get; set; }
}
Mapper.CreateMap<Foo, Foo1>()
    .ForMember(x => x.bar1, opt => opt.MapFrom(x => DateTime.Now)); // not using x, your function returns the value for bar1

I have to point out that this is not the way AutoMapper is designed to work. AutoMapper should map properties from one model to another. So if the data does not exist on modelA you should not map that data to modelB.

Your code change would be:

CreateMap<TeamEmployee, TeamEmployeeInput>()
    .ForMember(x => x.Teams, x => x.MapFrom(x => GetTeamEmployeeInputs()))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!