Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Automapper error #6059


User avatar
0
exlnt created

I am working on the AspNetZero .NET Core MVC JQuery, version 6.3.1. I am copying over the code from my existing AspNetZero solution into the new version one entity at a time. I realize this issue may not necessarily be an AspNetZero issue. However the code I'm using in this issue works perfectly in my Non-AspNetZero solution and is not working here in the AspNetZero solution. I have an entity named "LegalEntity" in my application. I have one property in my EditDto for this entity that does not exist in the entity or table, name "NewNote". When I run my app and goto the edit view for "LegalEntity" which uses the EditDto in its viewmodel, i get this AutoMapper error. I then applied this codeconfiguration.CreateMap<EditLegalEntityDto, LegalEntity>().ForSourceMember(dto => dto.NewNote, opt => opt.Ignore()); into the CustomDtoMapper class. Now when I run the application I get this auto-mapper error. I can see similar ignore mappings in the CustomDtoMapper class that were added by the AspNetZero team. I am following the same coding.

Can someone tell me what I am missing or doing wrong here? Thanks! Exlnt


2 Answer(s)
  • User Avatar
    0
    exlnt created

    I did some more digging into this issue and found a Stackoverflow thread that helped. I changed the mapping, as shown below. I reveresed the source and destination values and used ForMember instead. This resolved my error.

                configuration.CreateMap<LegalEntity, EditLegalEntityDto> ()
                    .ForMember(dto => dto.NewNote, opt => opt.Ignore());
    

    However, I would like to understand why the original code did not work? If anyone has any ideas, please do share.

  • User Avatar
    0
    aaron created
    Support Team

    Explained by @jbogard in AutoMapper/AutoMapper#1556#issuecomment-234586193:

    ForSourceMember is only meant for reverse-mapping scenarios or for situations where you're validating the source members. AutoMapper uses the destination members as the target to map to by default. You should use ForMember and Ignore.