Automapper 中的 ReverseMap 使用备用命名约定
ReverseMap in Automapper using alternate naming conventions
这是我的映射配置:
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 无效。
这是一个错误,你能打开一个 GitHub 问题吗?
这是一个解决方法(至少在错误修复之前):
Mapper.Initialize(config =>
{
config.ReplaceMemberName("_", "");
//Map as normal
});
这是我的映射配置:
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 无效。
这是一个错误,你能打开一个 GitHub 问题吗?
这是一个解决方法(至少在错误修复之前):
Mapper.Initialize(config =>
{
config.ReplaceMemberName("_", "");
//Map as normal
});