I have this model,
public class PatientRegistry { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Display(Name = "Record Id")] public long RecordId { get; set; } [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] [Display(Name = "Patient File Number")] public long PatientFileId { get; set; } [Required, StringLength(50)] [Display(Name = "First Name")] public string FirstName { get; set; } public int CountryCodeId { get; set; } public Country CountryCode { get; set; } public int CellProvider { get; set; } [Required, StringLength(15)] public string PatientMobileNo { get; set; } [StringLength(20)] public string PatientLandLine { get; set; } }
and my view model is,
public class PatientRegistryViewModel { public long PatientFileId { get; set; } [Required] [Display (Name = "First Name")] public string FirstName { get; set; } [Required] [Display (Name = "Last Name")] public string LastName { get; set; } public string AliasName { get; set; } [Required] [Display (Name = "Date of birth")] public DateTime PatientDob { get; set; } public MobilePhoneNumberViewModel PatientMobileNo { get; set; } }
my MobilePhoneNumberViewModel looks like,
public class MobilePhoneNumberViewModel { [Required] [Display (Name = "Country Code Id")] public int CountryCodeId { get; set; } [Required] [Display (Name = "Cell Provider")] public int CellProvider { get; set; } [Required] [Display (Name = "Mobile No")] public string PatientMobileNo { get; set; } }
my maps are,
CreateMap<PatientRegistry, PatientRegistryViewModel> () .ForMember (pr => pr.GenderId, opt => opt.MapFrom (m => m.Gender.Id)) .ForMember (pr => pr.StatusId, opt => opt.MapFrom (m => m.Status.Id)) .ForMember (pr => pr.CountryId, opt => opt.MapFrom (m => m.Country.Id)) .ForMember (pr => pr.StateId, opt => opt.MapFrom (m => m.State.Id)) .ForMember (pr => pr.CityId, opt => opt.MapFrom (m => m.City.Id)) .ForMember (pr => pr.NationalityId, opt => opt.MapFrom (m => m.Nationality.Id)) .ForMember (pr => pr.OccupationId, opt => opt.MapFrom (m => m.Occupation.Id)) .ForMember (pr => pr.MaritalStatusId, opt => opt.MapFrom (m => m.MaritalStatus.Id)) .ForMember (dest => dest.PatientMobileNo.PatientMobileNo, opt => opt.MapFrom (src => src.PatientMobileNo)) .ForMember (pr => pr.PatientMobileNo.CellProvider, opt => opt.MapFrom (m => m.CellProvider)) .ForMember (pr => pr.PatientMobileNo.CountryCodeId, opt => opt.MapFrom (m => m.CountryCodeId)) .ForMember (pr => pr.EthnicityId, opt => opt.MapFrom (m => m.Ethnicity.Id)) .ForMember (pr => pr.Partners, opt => opt.MapFrom (m => m.Partners));
and
CreateMap<PatientRegistry, MobilePhoneNumberViewModel> () .ForMember (pr => pr.CountryCodeId, opt => opt.MapFrom (m => m.CountryCodeId)) .ForMember (pr => pr.CellProvider, opt => opt.MapFrom (m => m.CellProvider)) .ForMember (pr => pr.PatientMobileNo, opt => opt.MapFrom (m => m.PatientMobileNo));
(I shortened my models for this question)
I keep getting this error,
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Expression 'dest => dest.PatientMobileNo.PatientMobileNo' must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead.
if I remove
.ForMember (dest => dest.PatientMobileNo.PatientMobileNo, opt => opt.MapFrom (src => src.PatientMobileNo)) .ForMember (pr => pr.PatientMobileNo.CellProvider, opt => opt.MapFrom (m => m.CellProvider)) .ForMember (pr => pr.PatientMobileNo.CountryCodeId, opt => opt.MapFrom (m => m.CountryCodeId))
AutoMapperMappingException: Error mapping types. Mapping types: PatientRegistry -> PatientRegistryViewModel ArtNgCore.Core.Models.ClinicCore.PatientRegistry -> ArtNgCore.Controllers.ViewModels.ClinicCoreViewModels.PatientRegistryViewModel Type Map configuration: PatientRegistry -> PatientRegistryViewModel ArtNgCore.Core.Models.ClinicCore.PatientRegistry -> ArtNgCore.Controllers.ViewModels.ClinicCoreViewModels.PatientRegistryViewModel Property: PatientMobileNo
whats going on here?