这是我的映射配置:
Mapper.Initialize(config =>
{
config.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
config.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
config.CreateMap<Rotator_Ad_Run, RotatorAdRunViewModel>()
.ReverseMap();
}
简化类:
public class Rotator_Ad_Run
{
public DateTime Start_Date { get; set; }
public DateTime End_Date { get; set; }
public bool Enabled { get; set; }
}
public class RotatorAdRunViewModel
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public bool Enabled { get; set; }
}
映射器从Rotator_Ad_Run
映射到RotatorAdRunViewModel
但反转时,它只映射Enabled
属性。当我使用.ForMember()
显式映射值时,它可以正常工作。
我是否需要做任何事情让Automapper知道命名约定需要颠倒?
更新
我正在尝试使用“个人档案”找到解决方法,但这些似乎也不起作用......
Mapper.Initialize(config =>
{
config.AddProfile<ModelToViewModel>();
config.AddProfile<ViewModelToModel>();
});
...
internal class ModelToViewModel : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
DestinationMemberNamingConvention = new PascalCaseNamingConvention();
this.CreateMap<Rotator_Ad_Run, RotatorAdRunViewModel>();
}
}
internal class ViewModelToModel : Profile
{
protected override void Configure()
{
SourceMemberNamingConvention = new PascalCaseNamingConvention();
DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
this.CreateMap<RotatorAdRunViewModel, Rotator_Ad_Run>();
}
}
ModelToViewModel配置文件有效,但ViewModelToModel没有。
答案 0 :(得分:3)
这是一种解决方法(至少在修复错误之前):
Mapper.Initialize(config =>
{
config.ReplaceMemberName("_", "");
//Map as normal
});
答案 1 :(得分:0)
这是一个错误,你能打开一个GitHub问题吗?
答案 2 :(得分:0)
配置文件解决方案有效。有关通过测试,请参阅Automapper: How to leverage a custom INamingConvention?和github问题。