2016-10-29 107 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 
    } 
}