Here is my mapping config:
Mapper.Initialize(config =>
{
config.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
config.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
config.CreateMap<Rotator_Ad_Run, RotatorAdRunViewModel>()
.ReverseMap();
}
And the simplified classes:
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; }
}
The mapper is mapping from Rotator_Ad_Run
to RotatorAdRunViewModel
but when reversed, it only maps the Enabled
property. It works when I explicitly map the values using .ForMember()
.
Is there anything I need to do to let Automapper know that the naming conventions need to be reversed?
UPDATE
I'm trying to find a workaround using Profiles, but those don't seem to work either...
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>();
}
}
The ModelToViewModel profile works, but the ViewModelToModel does not.
Here's a workaround (at least until the bug is fixed):
Mapper.Initialize(config =>
{
config.ReplaceMemberName("_", "");
//Map as normal
});
这是一个错误,你能打开一个GitHub问题吗?
The profiles solution works. See Automapper: How to leverage a custom INamingConvention? and the github issue for a passing test.
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.