fruitbatinshades
fruitbatinshades

Reputation: 131

AutoMapper treats known class instance as `Object`

I have a test that checks that objects match after conversion but automapper 10 has started saying I am missing a map from Object -> ExistingClass when the source object is defined.

Not an easy one to google, ahs anyone seen this before?

//maps defined elsewhere and checked
CreateMap<AnimalDBO, AnimalDTO>(MemberList.Destination)
CreateMap<AnimalDTO, AnimalBase>(MemberList.Destination)

var animal = new AnimalDBO
{
    Ear_Tag = "IE301228071641"
};

82: AnimalDTO dto = mapper.Map<AnimalDTO>(animal);
83: var ab = mapper.Map<AnimalBase>(dto);  //Automapper errors with Object -> AnimalBase

The object error:

AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping.
    
    Mapping types:
    Object -> AnimalBase
    System.Object -> HukNetCore.Animal.Domain.AnimalBase
  Stack Trace: 
    lambda_method(Closure , Object , AnimalBase , ResolutionContext )
    EarTagTests.StringToEarTagIE() line 83

I've tracked the error down to this method

private TDestination MapCore<TSource, TDestination>(
            TSource source, TDestination destination, ResolutionContext context, Type sourceType = null, Type destinationType = null, IMemberMap memberMap = null)
        {
            return ConfigurationProvider.GetExecutionPlan<TSource, TDestination>(MapRequest())(source, destination, context);
            MapRequest MapRequest()
            {
                var runtimeTypes = new TypePair(source?.GetType() ?? sourceType ?? typeof(TSource), destination?.GetType() ?? destinationType ?? typeof(TDestination));
                var requestedTypes = new TypePair(typeof(TSource), typeof(TDestination));
                return new MapRequest(requestedTypes, runtimeTypes, memberMap);
            }
        }

And these are the object created in that method

new TypePair(source?.GetType() ?? sourceType ?? typeof(TSource), destination?.GetType() ?? destinationType ?? typeof(TDestination))
"AnimalDTO", "AnimalBase"
    ContainsGenericParameters: false
    DestinationType: {Name = "AnimalBase" FullName = "Animal.Domain.AnimalBase"}
    IsGeneric: false
    IsGenericTypeDefinition: false
    SourceType: {Name = "AnimalDTO" FullName = "Animal.Domain.DTO.AnimalDTO"}
    SourceType: {Name = "AnimalDTO" FullName = "Animal.Domain.DTO.AnimalDTO"}
error CS0726: ':' is not a valid format specifier
new TypePair(typeof(TSource), typeof(TDestination))
"Object", "AnimalBase"
    ContainsGenericParameters: false
    DestinationType: {Name = "AnimalBase" FullName = "Animal.Domain.AnimalBase"}
    IsGeneric: false
    IsGenericTypeDefinition: false
    SourceType: {Name = "Object" FullName = "System.Object"}

Upvotes: 0

Views: 568

Answers (1)

fruitbatinshades
fruitbatinshades

Reputation: 131

Thanks Lucian,

In my case it turned out to be that when the DI was being set up I was not loading an assembly into memory before calling services.AddAutoMapper(refs);.

Check your ref list and check all the dlls your expect are loaded

Upvotes: 1

Related Questions