更新AutoMapper现在接收未映射的财产除外

更新AutoMapper现在接收未映射的财产除外

本文介绍了更新AutoMapper现在接收未映射的财产除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下映射:

  Mapper.CreateMap<播放列表,PlaylistDto>()
.ReverseMap( )
.ForMember(播放列表= GT; playlist.Folder,
选择=> opt.MapFrom(playlistDto = GT; folderDao.Get(playlistDto.FolderId)));



,其中转换的播放列表对象到PlaylistDto对象和背部。 ,似乎我更新AutoMapper之前工作的伟大



现在,当我打电话:

  Mapper.AssertConfigurationIsValid(); 



我见:

 发现未映射成员。查看下面的类型和成员。 
添加自定义映射表达式,忽略,添加自定义解析,或修改源/目标类型
====================== ===============================
PlaylistDto - >播放列表(来源成员列表)
Streamus.Dto.PlaylistDto - > Streamus.Domain.Playlist(来源成员列表)
------------------------------------ -----------------
FolderId

播放列表和PlaylistDto如下:

  [DataContract] 
公共类PlaylistDto
{
[数据成员(NAME =ID)]
公众的Guid标识{搞定;组; }

[数据成员(名称=标题)]
公共字符串名称{搞定;组; }

[数据成员(NAME =folderId)]
公众的Guid FolderId {搞定;组; }

[数据成员(名称=项目)]
公开名单< PlaylistItemDto>项目{搞定;组; }

[数据成员(NAME =序)]
公众诠释序列{搞定;组; }

公共PlaylistDto()
{
n = Guid.Empty;
标题=的String.Empty;
项=新的List< PlaylistItemDto>();
}

公共静态PlaylistDto创建(播放列表播放列表)
{
PlaylistDto playlistDto = Mapper.Map<播放列表,PlaylistDto>(播放);
返回playlistDto;
}
}

公共类播放列表:AbstractShareableDomainEntity
{
公共虚拟文件夹文件夹{搞定;组; }
//使用接口,以便NHibernate的可以用自己收藏的实施注入。
公共虚拟的ICollection< PlaylistItem>项目{搞定;组; }
公共虚拟INT序列{搞定;组; }

公开播放()
{
n = Guid.Empty;
标题=的String.Empty;
项=新的List< PlaylistItem>();
序列= -1;
}
}



为什么AutoMapper无法从文件夹中自动推导出FolderId ?更新后的



请注意,它仍然抱怨,即使我尝试显式定义映射到FolderId:

  Mapper.CreateMap<播放列表,playlistDto>()
.ForMember(播放列表=> playlist.FolderId,
选择=> opt.MapFrom(playlistDto => playlistDto.Folder.Id))
.ReverseMap()
.ForMember(播放列表= GT; playlist.Folder,
选择=> opt.MapFrom(playlistDto => folderDao.Get( playlistDto.FolderId)));


解决方案

答案是明确宣布我的映射:

  Mapper.CreateMap<播放列表,PlaylistDto>(); 
Mapper.CreateMap< PlaylistDto,播放列表>()
.ForMember(播放列表= GT; playlist.Folder,选择=> opt.MapFrom(playlistDto => folderDao.Get(playlistDto.FolderId)) );


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)));
解决方案

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)));

这篇关于更新AutoMapper现在接收未映射的财产除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:28