繁体   English   中英

如果使用命名约定,则Automapper会省略CreateMap

[英]Automapper omit CreateMap if naming convention is used

基于这个问题,我想知道是否可以省略CreateMap<PERSON, Person>();

我正在使用Automapper 7.0.1,配置文件具有以下形式

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

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

    }
}

我注意到,如果删除此行,则CreateMap<PERSON, Person>(); 那么只有不包含任何下划线的属性会被映射,并且映射器会抛出异常,并提示消息包含未映射的属性及其列表。

当我们只想基于命名约定映射所有属性时,为什么要使用CreateMap

编辑:

@Lucian回答后,我从Profile中删除了命名约定,并在初始化时添加了它,就像这样

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

现在,我不必使用CreateMap

默认地图(如果省略CreateMap,则由AM创建的默认地图)是全局的,因此肯定不在您创建的配置文件中。 因此,在个人资料上设置命名约定无济于事。 在全局配置上设置命名约定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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