简体   繁体   中英

How to configure AutoMapper to be case sensitive?

I expect the following test to fail, but it doesn't. How can I configure AutoMapper to be case sensitive?

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; }
    }
}

I'm using version 5.1.1 of AutoMapper .

Take a look at the naming convention configurations: https://github.com/AutoMapper/AutoMapper/wiki/Configuration#naming-conventions

At the Profile or Mapper level you can specify the source and destination naming conventions:

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

Or:

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

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