0

I’m using Automapper 6.1.1. and need to use reverse mapping. I found bug report from year 2004 and was closed. But in my example is not working, property c12 doesn't have value. So how could I use reverse mapping with this example?

public class Class1
{
    public string COSI_KDESI { get; set; }
}
public class Class2
{
    public string CosiKdesi { get; set; }
}
Mapper.Initialize(cfg =>
{
    cfg.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
    cfg.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
    cfg.CreateMap<Class1, Class2>().ReverseMap();
}); 

Class1 c1 = new Class1() { COSI_KDESI = "ttttttt" };

Class2 c2 = Mapper.Map<Class2>(c1);
Class1 c12 = Mapper.Map<Class1>(c2);
kubo
  • 492
  • 7
  • 19

1 Answers1

0

You need two different profiles, one configured as you already did and the other reversed.

Mapper.Initialize(cfg =>
{
    cfg.CreateProfile("p1", p=>
    {
        p.SourceMemberNamingConvention = new LowerUnderscoreNamingConvention();
        p.CreateMap<Class1, Class2>();
    });
    cfg.CreateProfile("p2", p=>
    {
        p.DestinationMemberNamingConvention = new LowerUnderscoreNamingConvention();
        p.CreateMap<Class2, Class1>();
    });
}); 
Lucian Bargaoanu
  • 3,336
  • 3
  • 14
  • 19
  • That’s sad that you cannot use that `reversmap()`. If you could define another naming convention for reverse mapping it would be very helpful. – kubo Aug 23 '17 at 14:12
  • I don't think that's possible because the conventions are different for the reverse map and the convention is per profile and that means the maps have to be in different profiles. – Lucian Bargaoanu Aug 23 '17 at 14:15
  • Yes I understand but it would be better have two different naming configuration in one profile so you don’t have to define those maps again. – kubo Aug 23 '17 at 14:26
  • You can request that on github. – Lucian Bargaoanu Aug 23 '17 at 14:28