Milad_Sh
Milad_Sh

Reputation: 51

How can I map these classes automapper?

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 .

Upvotes: 1

Views: 134

Answers (2)

Vishal
Vishal

Reputation: 26

Create a AutomapperProfile Helper class and add all the mapping in there .

    public class AutoMapperProfile : Profile 
    {
      public AutomapperProfile()
      {
      CreateMap<Person, Company>();

     }
     }

Upvotes: 0

Imesashvili_Irakli
Imesashvili_Irakli

Reputation: 306

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.

Upvotes: 1

Related Questions