Updated AutoMapper and now receiving unmapped property exception

跟風遠走 提交于 2019-12-07 12:08:27

问题


I have the following mapping:

Mapper.CreateMap<Playlist, PlaylistDto>()
      .ReverseMap()
      .ForMember(playlist => playlist.Folder,
          opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)));

Which converts a Playlist object to a PlaylistDto object and back. It seemed to work great before I updated AutoMapper.

Now, when I call:

Mapper.AssertConfigurationIsValid();

I see:

Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=====================================================
PlaylistDto -> Playlist (Source member list)
Streamus.Dto.PlaylistDto -> Streamus.Domain.Playlist (Source member list)
-----------------------------------------------------
FolderId

Playlist and PlaylistDto look like:

[DataContract]
public class PlaylistDto
{
    [DataMember(Name = "id")]
    public Guid Id { get; set; }

    [DataMember(Name = "title")]
    public string Title { get; set; }

    [DataMember(Name = "folderId")]
    public Guid FolderId { get; set; }

    [DataMember(Name = "items")]
    public List<PlaylistItemDto> Items { get; set; }

    [DataMember(Name = "sequence")]
    public int Sequence { get; set; }

    public PlaylistDto()
    {
        Id = Guid.Empty;
        Title = string.Empty;
        Items = new List<PlaylistItemDto>();
    }

    public static PlaylistDto Create(Playlist playlist)
    {
        PlaylistDto playlistDto = Mapper.Map<Playlist, PlaylistDto>(playlist);
        return playlistDto;
    }
}

public class Playlist : AbstractShareableDomainEntity
{
    public virtual Folder Folder { get; set; }
    //  Use interfaces so NHibernate can inject with its own collection implementation.
    public virtual ICollection<PlaylistItem> Items { get; set; }
    public virtual int Sequence { get; set; }

    public Playlist()
    {
        Id = Guid.Empty;
        Title = string.Empty;
        Items = new List<PlaylistItem>();
        Sequence = -1;
    }
}

Why is AutoMapper unable to automatically derive the FolderId from Folder after updating?

Note that it still complains even if I try to explicitly define the mapping to FolderId:

Mapper.CreateMap<Playlist, PlaylistDto>()
       .ForMember(playlist => playlist.FolderId,
            opt => opt.MapFrom(playlistDto => playlistDto.Folder.Id))
      .ReverseMap()
      .ForMember(playlist => playlist.Folder,
            opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)));

回答1:


The answer was to explicitly declare my mappings:

Mapper.CreateMap<Playlist, PlaylistDto>();
Mapper.CreateMap<PlaylistDto, Playlist>()
    .ForMember(playlist => playlist.Folder,opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)));


来源:https://stackoverflow.com/questions/21418024/updated-automapper-and-now-receiving-unmapped-property-exception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!