2016-10-29 54 views
1

我期望以下测试失败,但不会。我如何配置AutoMapper区分大小写?如何配置AutoMapper区分大小写?

public class AutomapperTests 
{ 
    [Fact] 
    public void CaseSensitiveTest() 
    { 
     Mapper.Initialize(cfg => cfg.AddMemberConfiguration().AddName<CaseSensitiveName>()); 

     Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>()); 

     Mapper.AssertConfigurationIsValid(); 
    } 

    public class Source 
    { 
     public int Foo { get; set; } 
    } 

    public class Destination 
    { 
     public int FoO { get; set; } 
    } 
} 

我正在使用版本5.1.1的AutoMapper

+0

可能[Automapper - 希望区分大小写]的副本(http://stackoverflow.com/questions/20600081/automapper-want-case-sensitive) – Operatorius

+0

@Operatorius在发布我的邮件之前,我已经看到了另一个问题,问题在于它没有真正的答案。只有2个链接到不适用(或不再适用)和第三个链接。 –

回答

0

看看命名约定的配置:https://github.com/AutoMapper/AutoMapper/wiki/Configuration#naming-conventions

在配置文件或映射器级别,您可以指定源和目的地命名约定:

Mapper.Initialize(cfg => { 
    cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention(); 
    cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention(); 
}); 

或者:

public class OrganizationProfile : Profile 
{ 
    public OrganizationProfile() 
    { 
    SourceMemberNamingConvention = new LowerUnderscoreNamingConvention(); 
    DestinationMemberNamingConvention = new PascalCaseNamingConvention(); 
    //Put your CreateMap... Etc.. here 
    } 
}