【发布时间】:2015-01-06 20:44:45
【问题描述】:
我有一个带有 Name 属性的 User 实体,我需要将它链接回 DTO 中的三个基于 Id 的属性。
用户实体
public string Name { get; set; }
public string SSN { get; set; }
相册实体
public string Title { get; set; }
public int Artist { get; set; } // <-- this ties to User.Id
public int Producer { get; set; } // <-- this ties to User.Id
public int Designer { get; set; } // <-- this ties to User.Id
public User User {get; set; }
相册DTO
public string Title { get; set; }
public int Artist { get; set; } // <-- this ties to User.Id
public int Producer { get; set; } // <-- this ties to User.Id
public int Designer { get; set; } // <-- this ties to User.Id
public string ArtistName { get; set; } // <-- need this to be User.Name
public string ProducerName { get; set; } // <-- need this to be User.Name
public string DesignerName { get; set; } // <-- need this to be User.Name
我正在尝试像这样映射它:
Mapper.CreateMap<Album, AlbumDto>()
.ForMember(dest => dest.ArtistName , opt => opt.MapFrom(s => s.User.Name));
但这只会引发映射错误(“找不到列 'User_Id'”)。
通过将 AlbumDto.Artist 与 User.Id 匹配,将 AlbumDto.ArtistName 与 User.Name 对齐的正确语法是什么?
【问题讨论】:
-
您确定这是 AutoMapper 问题吗?如果您在没有 AM 的情况下查询
s.User.Name会发生什么?
标签: automapper dto