0
public class SecurityAccess : IdentityUser    {
    public string LName { get; set; }
    public string FName { get; set; }
    public string MName { get; set; }
    public DateTime NameDate { get; set; }
    public DateTime DOB { get; set; }
    public string Spouse { get; set; }
    public string Comments { get; set; }
    public Address Address { get; set; }       
}

public class Address {
    public int Id { get; set; }
    public int OwnerId { get; set; }
    public DateTime CreationDate { get; set; }
    public Company Company { get; set; }
    public string Department { get; set; }
    public string Title { get; set; }
    public string Location{ get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
    public string Salutation { get; set; }
    public bool Active { get; set; }         
}

public class Company {
    public int Id { get; set; }
    public string Name { get; set; }        
    public DateTime CompanyDate { get; set; }    
}

public class ContactRegisterViewModel {
        public byte Id { get; set; }
        public String FName { get; set; }
        public String LName { get; set; }
        public String CompanyName { get; set; }
        public String AddressesTitle { get; set; }
        public String AddressesLocation { get; set; }
        public String AddressesCity { get; set; }
        public String AddressesState { get; set; }
        public String AddressesZip { get; set; }
        public String AddressesCountry { get; set; }
        public String AddressesPhone { get; set; }
        public String AddressesEmail { get; set; }
}

I have the following mappings set in Automapper

CreateMap<ContactRegisterViewModel, SecurityAccess>()
            .ForMember(dest => dest.Address,
                       opts => opts.MapFrom(
                            src => new Address
                            {
                                Address1 = src.AddressesAddress1,
                                Title = src.AddressesTitle,
                                City = src.AddressesCity,
                                State = src.AddressesState,
                                Zip = src.AddressesState,
                                Country = src.AddressesCountry,
                                Email = src.AddressesEmail,
                                Phone = src.AddressesPhone,

                            })).ReverseMap();

I am trying to convert the ContactRegisterViewModel to SecurityAccess. The conversion partially works. I am able to get the relevant properties filled in SecurityAccess including the Address property field that is in SecurityAccess model however I am struck trying to further map the ContactRegisterViewModel property CompanyName to the property in Address.Company.Name.

Any suggestions how i can map the entire object tree?.

1
  • any help, kinda a desperate for a solution ? Commented Jul 10, 2016 at 9:22

1 Answer 1

1

So one possible solution would go something like this:

CreateMap<ContactRegisterViewModel, SecurityAccess>()
    .ForMember(d => d.Address, o => o.MapFrom(s => s));

CreateMap<ContactRegisterViewModel, Address>()
    .ForMember(d => d.Title, o => o.MapFrom(s => s.AddressesTitle))
    .ForMember(d => d.Company, o => o.MapFrom(s => s));

CreateMap<ContactRegisterViewModel, Company>()
    .ForMember(d => d.Name, o => o.MapFrom(s => s.CompanyName));

This should get the job done, although it's probably not the most elegant. Might be worth a shot looking into those ConstructUsing / ConvertUsing methods (see here).

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

2 Comments

You are a lifesaver, thank you @AlexandruMarculescu that was perfect, worked like a charm!!
If i throw a little curve ball and make The Address in SecurityAccess public IEnumerable<Address> Addresses { get; set; } and Company in Address table public IEnumerable<Company> Companies{get; set;} How will the mapping be done?

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.