Jason
Jason

Reputation: 527

ReverseMap in Automapper using alternate naming conventions

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.

Upvotes: 2

Views: 692

Answers (2)

Jason
Jason

Reputation: 527

Here's a workaround (at least until the bug is fixed):

Mapper.Initialize(config =>
{
    config.ReplaceMemberName("_", "");
    //Map as normal
});

Upvotes: 3

Jimmy Bogard
Jimmy Bogard

Reputation: 26785

This is a bug, can you open a GitHub issue?

Upvotes: 0

Related Questions