Using Automapper

Is anyone else using Automapper to map Kentico Models to ViewModels? I'm having a heck of a time with items that have structured rich text.

An unhandled exception occurred while processing the request.
InvalidCastException: Unable to cast object of type 'System.Collections.Generic.List`1[KenticoCloud.Delivery.IRichTextBlock]' to type 'KenticoCloud.Delivery.IRichTextContent'.

AutoMapperMappingException: Error mapping types.

I figure I need to make custom converter, but I'm not sure how with interfaces.

Tagged:

Best Answer

  • Accepted Answer

    Added this to the AutoMapper profile and it seems to work now:
    CreateMap<IRichTextContent, IRichTextContent>().ConvertUsing(s => s);

Answers

  • mmandersonmmanderson Member
    edited December 6

    I guess the first part I don't understand is why Automapper is converting IRichTextContent to List in my Model and then trying to map it to IRichTextContent in my ViewModel. From what I read, Automapper shouldn't do that if the properties have the same name, and they do.

    I have tried this map:
    CreateMap<IEnumerable<IRichTextBlock>, IRichTextContent>() .ForMember(x => x.Blocks, m => m.MapFrom(x => x));

    But then I get: TypeLoadException: Method 'GetEnumerator' in type 'Proxy_KenticoCloud.Delivery.IRichTextContent_63220684_' from assembly 'AutoMapper.Proxies, Version=0.0.0.0, Culture=neutral, PublicKeyToken' does not have an implementation.

  • mmandersonmmanderson Member

    From what I can gather, Automapper needs to know the concrete type to map IRichTextContent, but this is an internal class in the SDK.

Sign In or Register to comment.