我有一种情况,我希望使用较低的下划线向pascal转向一种方式,但pascal以较低的方式强调另一种方式。我的理解是,配置文件可以做到这一点,但我正在努力让它工作。这就是我所拥有的:
Mapper.Initialize(cfg =>
{
cfg.AddProfile<FromUnderscoreMapping>();
cfg.AddProfile<ToUnderscoreMapping>();
});
Mapper.CreateMap<ArticleEntity, Article>().WithProfile("FromUnderscoreMapping");
...
public class FromUnderscoreMapping : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
}
public override string ProfileName
{
get { return "FromUnderscoreMapping"; }
}
}
public class ToUnderscoreMapping : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new PascalCaseNamingConvention();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
}
public override string ProfileName
{
get { return "ToUnderscoreMapping"; }
}
}
答案 0 :(得分:6)
需要将映射移动到配置:
public class FromUnderscoreMapping : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
CreateMap<ArticleEntity, Article>();
}
public override string ProfileName
{
get { return "FromUnderscoreMapping"; }
}
}
public class ToUnderscoreMapping : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new PascalCaseNamingConvention();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
CreateMap<Article, ArticleEntity>();
}
public override string ProfileName
{
get { return "ToUnderscoreMapping"; }
}
}