skip to Main Content

how can I map Person to Company:

public class Person
{
    public Guid Id { get; set;}
    public string Name { get; set;}
    public string Country { get; set;}
    public string PhoneNumber { get; set;}
}

public class Company
{
    public List<Member> Members { get; set; }
    public string Name { get; set;}
    
}

public class Member
{
    public Guid Id { get; set;}
    public string FullName { get; set; }
}

I tried to do it with auto mapper but I couldn’t success .

2

Answers


  1. I assume when mapping Person to Member, you want the Person.Name and Member.FullName to be the mapped.

    So for that I would do this.

    CreateMap<Person, Member>()
                    .ForMember(
                        x => x.FullName,
                        opt => opt.MapFrom(src => src.FullName)
                        );
    

    As for mapping Person to Company, I really don’t understand why you would map these two if you already have Person and Member mapped.

    Login or Signup to reply.
  2. Create a AutomapperProfile Helper class and add all the mapping in there .

        public class AutoMapperProfile : Profile 
        {
          public AutomapperProfile()
          {
          CreateMap<Person, Company>();
    
         }
         }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search