55

This might be a stupid question! (n00b to AutoMapper and time-short!)

I want to use AutoMapper to map from EF4 entities to ViewModel classes.

1) If I call

CreateMap<ModelClass, ViewModelClass>()

then do I also need to call

CreateMap<ViewModelClass, ModelClass>()

to perform the reverse?

2) If two classes have the same property names, then do I need a CreateMap statement at all, or is this just for "specific/custom" mappings?

BlueChippy
  • 5,935
  • 16
  • 81
  • 131
  • 8
    Use `CreateMap().ReverseMap()` with all the chained trimmings will save you some keystrokes -- see http://stackoverflow.com/questions/13490456/automapper-bidirectional-mapping-with-reversemap-and-formember – drzaus Jul 23 '14 at 18:19

4 Answers4

139

For the info of the people who stumble upon this question. There appears to be now a built-in way to achieve a reverse mapping by adding a .ReverseMap() call at the end of your CreateMap() configuration chain.

Ivan Zlatev
  • 13,016
  • 9
  • 41
  • 50
  • 6
    Apparently it was included [a couple months after the first answer](https://github.com/AutoMapper/AutoMapper/commit/bff6e2aa49af3e7b50f527376da48924efa7d81e) – drzaus Jul 23 '14 at 18:18
  • `.ReverseMap()` doesn't work for `.ForMember(dest => dest.prop, opt => optMapFrom(src => src.prop))` – bahramzy Feb 04 '21 at 11:49
19

In AutoMapper you have a Source type and a Destination type. So you will be able to map between this Source type and Destination type only if you have a corresponding CreateMap. So to answer your questions:

  1. You don't need to define the reverse mapping. You have to do it only if you intend to map back.
  2. Yes, you need to call CreateMap to indicate that those types are mappable otherwise an exception will be thrown when you call Map<TSource, TDest> telling you that a mapping doesn't exist between the source and destination type.
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I know this is not the place to ask questions. Usually I'm using AutoMapper to initiate an Entity object with DTO and DTO is the only model I use in client application. My question is about reading from Database, In that time I've a collection of Entity model which should initiate DTO model. Do I need to create another reverse mapping ? – Saber Amani Jan 11 '13 at 17:07
  • @saber yes. I mean that's what I am doing unless someone else proofs the reverse. – Teoman shipahi Aug 04 '15 at 04:55
  • The question still remains, WHY do we have to call `CreateMap`? Forcing me to manually designate mapping isn't being true to the whole "auto" part of things... – Slight Apr 29 '16 at 23:10
10

I've used an extension method do mapping both ways

    public static IMappingExpression<TDestination, TSource> BothWays<TSource, TDestination>
        (this IMappingExpression<TSource, TDestination> mappingExpression)
    {
        return Mapper.CreateMap<TDestination, TSource>();
    }

usage:

 CreateMap<Source, Dest>().BothWays();
Brian
  • 1,845
  • 1
  • 22
  • 37
  • 4
    I think this extension already exists as `.ReverseMap()` i.e. `CreateMap().ReverseMap()` as seen [here](http://stackoverflow.com/a/18490071/1037948)...and as I just noticed on [the answer below](http://stackoverflow.com/a/11673804/1037948) – drzaus Jul 23 '14 at 18:15
  • I think ReverseMap was added after Feb 2012. – Brian Jul 23 '14 at 19:42
  • maybe, I'm just going by [the commit timestamp](https://github.com/AutoMapper/AutoMapper/commit/bff6e2aa49af3e7b50f527376da48924efa7d81e) of Nov 2011 – drzaus Jul 24 '14 at 17:23
1
  1. Yes, or you can call CreateMap<ModelClass, ViewModelClass>().ReverseMap().
  2. If two classes have same Member(Property,Field,GetMethod()), you needn't call CreateMap<TSrc,TDest>. Actually, if every member in TDest are all exist in TSrc, you needn't call CreateMap<TSrc,TDest>. The following code works.
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }  
}
class Person2
{
   public string Name { get; set; }
   public int? Age { get; set; }
   public DateTime BirthTime { get; set; }
}
public class NormalProfile : Profile
{
    public NormalProfile()
    {
       //CreateMap<Person2, Person>();//
    }
}
   
var cfg = new MapperConfiguration(c => 
{ 
    c.AddProfile<NormalProfile>();
});
//cfg.AssertConfigurationIsValid();
var mapper = cfg.CreateMapper();
var s3 = mapper.Map<Person>(new Person2 { Name = "Person2" });
Jim
  • 489
  • 5
  • 11