1

I have a View Model such as

public class RootViewModel
{
    public CreateCompanyViewModel Company { get; set; }

    public string Name { get; set; }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }

    public CreateUserTypeViewModel UserType { get; set; }
}

And CreateCompanyViewModel and CreateUserTypeViewModel are like

public class CreateCompanyViewModel
{
    public string CompanyName { get; set; }
}

public class CreateUserTypeViewModel
{
    public string UserTypeName { get; set; }
}

I want this RootVM to be flattened to multiple DTO's. The 3 DTO's for the above RootVM I have are like

public class UserDTO
{
    public string Name { get; set; }

    public string Email { get; set; }

    public string PhoneNumber { get; set; }

    public string Password { get; set; }

    public string ConfirmPassword { get; set; }
}

public class CompanyDTO
{
    public string CompanyName { get; set; }
}

public class UserTypeDTO
{
    public string UserTypeName { get; set; }
}

NOTE : Note that CompanyDTO and UserTypeDTO are not nested object (part of) UserDTO unlike RootVM.

When I'm doing the mapping using AutoMapper RootVM properties gets mapped to UserDTO but CompanyDTO and UserTypeDTO are null as expected.

I tried mapping them by using ForMember function with MapFrom and ResolveUsing methods, but both of them shows error as

Custom configuration for members is only supported for top-level individual members on a type.

UPDATE Below is my mapping code

CreateMap<RootViewModel, CompanyDTO>();
CreateMap<RootViewModel, UserDTO>();
CreateMap<RootViewModel, UserTypeDTO>();
CreateMap<CreateCompanyViewModel, CompanyDTO>();
CreateMap<CreateUserTypeViewModel, UserTypeDTO>();

I'm using AutoMapper 5.2.0

UPDATE - Fix : Well what I found is, either I have to use .ForMember for all the properties manually, else for automatic convention to work, I need to use https://github.com/AutoMapper/AutoMapper/wiki/Flattening or https://arnabroychowdhurypersonal.wordpress.com/2014/03/08/flattening-object-with-automapper/.

This is the only way to make it work.

Wish I could do .ForMember(d => d, s => s.MapFrom(x => x.Company)) and it'd map all the properties from CreateCompanyViewModel => CompanyDTO. This would have been very handy, but AutoMapper doesn't supports this.

9
  • have you added mapping for company CompanyDTO and CreateCompanyViewModel separately ? Commented Jan 28, 2017 at 8:50
  • I have added mapping for RootViewModel => UserDTO, RootViewModel => CompanyDTO, RootViewModel => UserTypeDTO, CreateCompanyViewModel => CompanyDTO, CreateUserTypeViewModel => UserTypeDTO Commented Jan 28, 2017 at 8:52
  • remove RootViewModel => CompanyDTO and RootViewModel => UserTypeDTO and try Commented Jan 28, 2017 at 8:54
  • No it doesn't works as I'm trying to get a CompanyDTO from RootViewModel and removing the mapping throws error as No Mapping Configuration Exists from RootViewModel => CompanyDTO Commented Jan 28, 2017 at 8:57
  • Well can you post your mapping code ? Commented Jan 28, 2017 at 9:02

1 Answer 1

2

Try following

        CreateMap<CreateCompanyViewModel, CompanyDTO>();
        CreateMap<CreateUserTypeViewModel, UserTypeDTO>();

        CreateMap<RootViewModel, CompanyDTO>()
            .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Company.CompanyName));

        CreateMap < RootViewModel, UserTypeDTO()
              .ForMember(dest => dest.UserTypeName, opt => opt.MapFrom(src => src.UserType.UserTypeName));

        CreateMap<RootViewModel, UserDTO>();
Sign up to request clarification or add additional context in comments.

1 Comment

It's working this way, but I have to now write around 30 properties manually. I was looking for some conventional way (automatic). I think I have found one but it won't apply to my case. arnabroychowdhurypersonal.wordpress.com/2014/03/08/…

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.