3
votes

I have a model which I am trying to map from Match class in .net core 2.0. Both the classes have a Name property.

I need to map Match.Value => ViewCompany.Name

But it always puts Match.Name into ViewCompany.Name

Here is my AutomapperProfile:

CreateMap<Match, ViewCompany>()
                .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Value));

.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Value))

ViewCompany:

public class ViewCompany
{
    public ViewCompany()
    {

    }

    public ViewCompany(string name)
    {
        this.Name = name;
    }

    public int Id { get; set; }

    public string Name { get; set; }
}

The above mapping doesn't work.

But if I change property name in the model to something else like "Value" or "tempName" and update the automapper profile, it works fine.

So, is it not possible to map properties with same names to different properties in Automapper?

1
Works for me, try upgrading.Lucian Bargaoanu
@LucianBargaoanu I have the latest 6.0.0 version of Automapper.Extensions.Microsoft.DependencyInjectionSachin Parashar
Most likely your configuration is ignored because you're not setting it up properly. Check the docs.Lucian Bargaoanu
@LucianBargaoanu Configuration seems fine because I changed property "Name" to "CompanyName" and updated configuration .ForMember(dest => dest.CompanyName, opt => opt.MapFrom(src => src.Value)) And it worked.Sachin Parashar

1 Answers

3
votes

What happens here is that Name is mapped through the constructor. A simple way to avoid that is to tell AM what constructor to use:

 CreateMap<Match, ViewCompany>().ConstructUsing(source=>new ViewCompany());