我有以下映射:
Mapper.CreateMap<Playlist, PlaylistDto>()
.ReverseMap()
.ForMember(playlist => playlist.Folder,
opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)));
将播放列表对象转换为PlaylistDto对象并返回。在更新AutoMapper之前,它似乎运行良好。
现在,当我打电话:
Mapper.AssertConfigurationIsValid();
我明白了:
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
播放列表和播放列表D看起来像:
[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无法在更新后自动从文件夹派生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)));
答案 0 :(得分:1)
答案是明确声明我的映射:
Mapper.CreateMap<Playlist, PlaylistDto>();
Mapper.CreateMap<PlaylistDto, Playlist>()
.ForMember(playlist => playlist.Folder,opt => opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)));