1

In order to save changes to my db, I am using Automapper to map a property of type string from destination to a property of type int in source.

I have tried ForMember but it works for source to destination only.

So how can I map a property named IpPort of type string in class B to a property named IpPort of type int in class A.

Class A and B are mapped like this:

cf.CreateMap<classA, classB>().ReverseMap();
CDspace
  • 2,639
  • 18
  • 30
  • 36
Arh Hokagi
  • 170
  • 5
  • 21
  • something like `ForMember(dest => dest.IpPort , opt => opt.MapFrom(src => int.Parse(src.IpPort )));` – Aria Nov 08 '17 at 15:59
  • @Aria that works for source to destination, I want the oposite – Arh Hokagi Nov 08 '17 at 16:02
  • 2
    Possible duplicate of [Automapper: bidirectional mapping with ReverseMap() and ForMember()](https://stackoverflow.com/questions/13490456/automapper-bidirectional-mapping-with-reversemap-and-formember) – DaniCE Nov 08 '17 at 16:02

1 Answers1

1

As the AutoMapper main site said you can use ForPath for customizing reverse map .

So you can use ForPath liek below:

 CreateMap<ClassA, ClassB>()
    .ForMember(d => dest.IpPort , opt => opt.MapFrom(src => int.Parse(src.IpPort )));
    .ReverseMap()
    .ForPath(s => s.IntPort, opt => opt.MapFrom(src => src.IntPort.ToString()));

or you can use AferMap and check the type of source and destination type like

AutoMapper.CreateMap<ClassA, ClassB>().ReverseMap().AfterMap((source, destination) =>
        {
            object ob = (object)source;
           string type= ob.GetType().ToString();
            if(type == "ClassA")
                //Do something
             else
                //Do something
        })
Aria
  • 3,724
  • 1
  • 20
  • 51