Reputation: 185
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?.
Upvotes: 0
Views: 90
Reputation: 5770
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).
Upvotes: 1