7

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
  • I have a similar situation with a DateTime object.Date mapping to/from an object.SubClass.Date value...I'm getting an 'Unable to cast object of type 'System.DateTime' to type 'Object.SubClass' error but have been attempting the exact same mapping init...any sugguestions? Commented Aug 28, 2012 at 15:40

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.