我正在尝试使用 automapper 将一些嵌套列表映射到扁平结构列表中。目前,我能够用名称映射正确数量的学分,但我无法获得要映射的角色和角色描述。
我正在尝试获得这样的扁平列表。
约翰史密斯,演员,邪恶博士
约翰史密斯,作家,空
弗兰克·布朗,董事,空
乔·格林,演员,追随者
这是我的域模型、视图模型和映射的缩减版本。我在使用 Domain.Credit 到 ViewModel.Credit 映射时做错了什么?
//Domain Model
public class Talent
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual IList<Credit> Credits { get; set; }
}
public class Credit
{
public virtual Role Role { get; set; }
public virtual string Subtitle { get; set; }
}
public class Role
{
public virtual string Title { get; set; }
}
//View Model
public class EpisodeViewModel
{
public IList<Credit> Credits { get; set; }
public class Credit
{
public string Name { get; set; }
public string Role { get; set; }
public string RoleDescription { get; set; }
}
}
//Mappings
Mapper.CreateMap<Episode, EpisodeViewModel>()
.ForMember(destination => destination.Credits, options => options.MapFrom(source => source.Talent));
Mapper.CreateMap<Talent, Credit>()
.ForMember(destination => destination.Name, options => options.MapFrom(source => string.Format("{0} {1}", source.FirstName, source.LastName)));
Mapper.CreateMap<Domain.Credit, Credit>()
.ForMember(destination => destination.Role, options => options.MapFrom(source => source.Role.Title))
.ForMember(destination => destination.RoleDescription, options => options.MapFrom(source => source.Subtitle));