1

我有以下对象:

public class DomainStudent {
    public long Id { get; set; }
    public string AdvisorId { get; set; }
}

public class ApiStudent {
    public long Id { get; set; }
    public long AdvisorName { get; set; }
}

当我运行以下映射时:

ApiStudent api = new ApiStudent();
api.Id = 123;
api.AdvisorName = "Homer Simpson";

DomainStudent existing = service.load(api.Id); // 123
// at this point existing.AdvisorId = 555

existing = Mapper.Map<ApiStudent, DomainStudent>(api);
// at this point existing.AdvisorId = null

如何配置 AutoMapper,以便当AdvisorId源对象中缺少该属性时,它不会被覆盖为 null?

4

1 回答 1

3

您必须将 Map() 调用更改为:

Mapper.Map(api, existing);

然后将映射配置为:

 Mapper.CreateMap<ApiStudent, DomainStudent>()
            .ForMember(dest => dest.AdvisorId, opt => opt.Ignore());
于 2013-04-10T18:41:39.687 回答