我有以下映射:
Mapper.CreateMap<Playlist, PlaylistDto>()
.ReverseMap()
.ForMember(playlist => playlist.Folder,
opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)));
它将 Playlist 对象转换为 PlaylistDto 对象并返回。在我更新 AutoMapper 之前,它似乎工作得很好。
现在,当我打电话时:
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 和 PlaylistD 看起来像:
[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;
}
}
为什么AutoMapper更新后无法自动从Folder中派生FolderId?
请注意,即使我尝试显式定义到 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)));