简体   繁体   中英

Automapper omit CreateMap if naming convention is used

Based on this question, I want to know if I can omit CreateMap<PERSON, Person>();

I am using Automapper 7.0.1 and the profile has the following form

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        //...
        //...

        SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
        DestinationMemberNamingConvention = new PascalCaseNamingConvention();
        CreateMap<PERSON, Person>(); //what I want to omit

    }
}

I have noticed that if I remove this line CreateMap<PERSON, Person>(); then only properties that don't contain any underscore get mapped and the mapper throws exception with the message that there are unmapped properties and a list of them.

Why do we want to use CreateMap when we want to map all properties based only on naming convention?

EDIT:

After @Lucian answer I removed naming convention from Profile and added it at initialization like this

Mapper.Initialize(cfg =>
{
    cfg.AddProfile<AutoMapperProfile>();
    cfg.SourceMemberNamingConvention = new UpperUnderscoreNamingConvention();
    cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
});

Now I don't have to use CreateMap .

A default map (the one created by AM if you omit CreateMap) is global, it's certainly not in that profile you created. So setting the naming conventions on your profile doesn't help. Set the naming conventions on the global configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM