将地图模型映射到ViewModel时,AutoMapper中的InCorrect Map

时间:2016-06-06 20:25:39

标签: c# asp.net-mvc mvvm automapper

我有一个模型" TClientsAdmins"和ViewModel是TClientsAdminsViewModel.I想要读取DB中过滤ID的所有记录。 我从域模型中读取它们,当我想要Map到viewmodel时,我在viewmodel中得到零。 另外,我有一个名为THoldingAdmins的模型,它与TClientsAdmins相关联。

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int Id { get; set; }

    [StringLength(50)]
    public string CompanyName { get; set; }


   public int? THoldingAdminsId { get; set; }


   public virtual THoldingAdmins THoldingAdmins { get; set; }

和TClientAdminViewModel:

    public int Id { get; set; }

    [StringLength(50)]
    public string CompanyName { get; set; }


    public int? THoldingAdminsId { get; set; }


    public virtual THoldingAdminsViewModel THoldingAdmins { get; set; }

和THOLdingAdmins:

    public byte[] Logo { get; set; }


    public bool IsDeleted { get; set; }


    public virtual List< TClientsAdmins> TClientsAdmins { get; set; }

和AutoMapper配置:

 Mapper.CreateMap<TClientsAdminsViewModel, TClientsAdmins>();
 Mapper.CreateMap<List<TClientsAdminsViewModel>,List<TClientsAdmins>>();

1 个答案:

答案 0 :(得分:0)

您不需要IList的显式映射,但如果您正在从Model映射到ViewModel,那么看起来您的映射方式是错误的。尝试

Mapper.CreateMap<TClientAdmins, TClientAdminsViewModel>();
Mapper.CreateMap<THoldingAdmins, THoldingAdminsViewModel>();