自动将c#集合到对象中

时间:2017-05-03 14:42:22

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

我遇到了c#mvc和automapper的问题。 我有一些模特,这是一个:

public class UserProfileModel
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public UserProfileModel()
    {
        this.Profili_Allocati = new List<Profili_AllocatiModel>();
        this.Tbl_SchedaValutativa = new List<Tbl_SchedaValutativaModel>();
    }
    [Key]
    [Column(Order = 0)]
    public int Id { get; set; }
    [StringLength(150, ErrorMessage = "This field should not exceed 150 characters")]
    [Required(AllowEmptyStrings = false)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Nome { get; set; }
    [StringLength(150, ErrorMessage = "This field should not exceed 150 characters")]
    [Required(AllowEmptyStrings = false)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Cognome { get; set; }
    [Required(AllowEmptyStrings = false)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public int Specializzazione { get; set; }
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:mm/dd/yyyy}")]
    public Nullable<System.DateTime> DataNascita { get; set; }
    [StringLength(256, ErrorMessage = "This field should not exceed 256 characters")]
    [Required(AllowEmptyStrings = false)]
    [DisplayFormat(ConvertEmptyStringToNull = false)]
    public string Email { get; set; }
    [StringLength(1, ErrorMessage = "This field should not exceed 1 characters")]
    public string Gender { get; set; }
    [StringLength(50, ErrorMessage = "This field should not exceed 50 characters")]
    public string Telefono { get; set; }

    public CodeTableModel CodeTable { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public List<Profili_AllocatiModel> Profili_Allocati { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public List<Tbl_SchedaValutativaModel> Tbl_SchedaValutativa { get; set; }
}

这个映射代码(来自数据库):

  Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<CodeTable, CodeTableModel>()
            .ForMember(lc => lc.UserProfile, opt => opt.Ignore())
          .ForMember(lc => lc.Tbl_OpportunitaSkill, opt => opt.Ignore())
          .ForMember(lc => lc.CodeTableType, opt => opt.MapFrom(rc => rc.CodeTableType));
            cfg.CreateMap<Opportunita, OpportunitaModel>()
             .ForMember(lc => lc.Tbl_OpportunitaSkill, opt => opt.Ignore());
            cfg.CreateMap<CodeTableType, CodeTableTypeModel>();
            cfg.CreateMap<UserProfile, UserProfileModel>()
              .ForMember(lc => lc.Profili_Allocati, opt => opt.Ignore())
          .ForMember(lc => lc.Tbl_SchedaValutativa, opt => opt.Ignore())
          .ForMember(lc => lc.CodeTable, opt => opt.MapFrom(rc => rc.CodeTable));
            cfg.CreateMap<Tbl_OpportunitaSkill, Tbl_OpportunitaSkillModel>()
          .ForMember(lc => lc.Opportunita, opt => opt.MapFrom(rc => rc.Opportunita))
          .ForMember(lc => lc.CodeTable, opt => opt.MapFrom(rc => rc.CodeTable));
            cfg.CreateMap<Vw_OpportunitaSkill, Vw_OpportunitaSkillModel>();
            cfg.CreateMap<Vw_Opportunita, Vw_OpportunitaModel>();
            cfg.CreateMap<Vw_CheckProfile, Vw_CheckProfileModel>();
            cfg.CreateMap<Profili_Allocati, Profili_AllocatiModel>()
        .ForMember(lc => lc.UserProfile, opt => opt.MapFrom(rc => rc.UserProfile));

        });

当我尝试进入菜单时,我得到了不支持的映射的例外,我该怎么办?

0 个答案:

没有答案