AutoMapper How to map nested object from an ObjectId

荒凉一梦 提交于 2019-12-05 23:08:28

问题


I am trying to map the ReferralContract.AssessmentId property to Referral.Assessment.Id The below code works but I am sure that there is a cleaner way to do.... Please tell me this is so ;-)

// Destination classes
public class Referral
{
    public Referral()
    {
        Assessment = new Assessment();
    }

    public int Id { get; set; }
    public Assessment Assessment { get; set; }
}

public class Assessment
{
    public int Id { get; set; }
}

// Source Class
public class ReferralContract
{
    public int Id { get; set; }
    public int AssessmentId { get; set; }
}

The Automapper mapping I am using is

Mapper.CreateMap<ReferralContract, Referral>()
      .ForMember(x => x.Assessment,
          opt => opt.MapFrom(scr => new Assessment { Id = scr.AssessmentId }));

回答1:


For now, that's the cleanest way to go. AutoMapper's design is not optimized for these reverse-mapping scenarios, but that's something I'm looking at for future versions.



来源:https://stackoverflow.com/questions/2634253/automapper-how-to-map-nested-object-from-an-objectid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!