1

我想使用自动映射器将模型集合映射到视图模型这是我的代码

public class LanguageViewModel
    {
        public string Name { get; set; }
        public IList<Word> Categories { set; get; }
        public IList<string> PictureURL { set; get; }
    }

这是我的模型

public class Word : BaseFieldsTables
{
    public string Text { get; set; }
    public virtual Category Category { get; set; }
    public int CategoryID { get; set; }
    public virtual Language Language { get; set; }
    public int LanguageID { get; set; }

    public virtual ICollection<Picture> Pictures { get; set; }

    [InverseProperty("MainWord")]
    public virtual ICollection<RelationshipBetweenWords> MainWords { get; set; }

    [InverseProperty("RelatedWord")]
    public virtual ICollection<RelationshipBetweenWords> RelatedWords { get; set; }
}
4

1 回答 1

0

在他的示例中,您从顶级域模型开始并将其映射到称为嵌套映射的内部集合。

mapper.CreateMap<Word , LanguageViewModel>()                
    .ForMember(dest => dest.Name, src => src.MapFrom(s => s.Text));

对于 Word 域模型中的内部集合,您需要为类别和图片定义单独的映射。

mapper.CreateMap<Picture, PictureModel>()                
   .ForMember(dest => dest.PictureURL , src => src.MapFrom(x => x.Picture.Urltext));

mapper.CreateMap<Category, CategoryModel>()                
   .ForMember(dest => dest.CategoryId, src => src.MapFrom(x => x.Category.CategoryInt));

对于所有剩余的内部集合,您需要定义映射以使其工作

于 2012-06-28T00:16:00.833 回答