grokky grokky - 6 days ago 6
C# Question

AutoMapper mapping from type to type with nested type

I'm using the latest version, with the instance API.

My source type:

public class Source {
public string ValueX { get; set; }
public string ValueY { get; set; }
public string ValueZ { get; set; }
}


My destination type:

public class Destination {

public Destination () { Inner = new Inner(); }

public string ValueX { get; set; }

public Inner Inner { get; set; }

public class Inner {
public string ValueY { get; set; }
public string ValueZ { get; set; }
}

}


My config (done in a profile):

CreateMap<Source, Destination>()
.ForMember(dest => dest.Inner, opt => opt.MapFrom(src => src));


But this doesn't map the inner object. When I test the config, it throws an unhelpful
AutoMapperConfigurationException
exception.

What is the proper way to make this mapping work?

Answer

Before doing that you need to define a map between Source and Inner like:

CreateMap<Source, Inner>();
Comments