4

Using Automapper 5.0.2.0 I am attempting to map From TypeA to TypeB:

public class TypeA
{
    public double Length { get; set; }
}


public class TypeB
{
    public Distance Length { get; set; }
}

I make the assumption that the Length is stored in inches and have created this mapping profile:

public class CalculationProfile : Profile
{
    public CalculationProfile()
    {
       CreateMap<TypeA, TypeB>()
            .ForMember(dest => dest.Length,
                       opt => opt.MapFrom(src => new Distance(src.Length, "Inch")))
    }
}

and I use it as such:

Mapper.Initialize(configuration =>
{
    configuration.AddProfile(new CalculationProfile());
});

var typeA = new TypeA(){Length = 1.0};

var typeB = Mapper.Map<TypeB>(typeA);

However that last line throws the following error:

AutoMapper.AutoMapperMappingException was unhandled by user code
  HResult=-2146233088
  Message=Missing type map configuration or unsupported mapping.

Mapping types:
Double -> Distance
System.Double -> UnitClassLibrary.DistanceUnit.Distance
  Source=Anonymously Hosted DynamicMethods Assembly
  StackTrace:
       at lambda_method(Closure , Double , Distance , ResolutionContext )
       at lambda_method(Closure , Object , Object , ResolutionContext )
       at TestProject.AutoMapperTests.FromPersistenceObjectToCalculationModel_Test() in C:\...\AutoMapperTests.cs:line 69
  InnerException: 

This seems like a super simple case for Automapper to handle, however I seem unable to fix the error. Any suggestions are appreciated.

jth41
  • 3,808
  • 9
  • 59
  • 109

1 Answers1

0

You really want a new type conversion between double and distance:

CreateMap<double, Distance>().ConvertUsing(src => new Distance(src, "Inch"));
Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69