0

I have several hours trying to map between the following types:

Source Type:

    public class PatientModel : IPatientModel
    {
        public int Id { get; set; }

        public int? PatientNumber { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public DateTime BirthDate { get; set; }

        public int? Gender { get; set; }        

        public string Job { get; set; }

        public byte[] RowVersion { get; set; }

        public DateTime? LastVisitDate { get; set; }
    }

Destination Type:

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

        [Index(IsUnique = true)]
        public int? PatientNumber { get; set; }

        [Required]
        [MaxLength(50)]
        public string FirstName { get; set; }

        [Required]
        [MaxLength(50)]
        public string LastName { get; set; }

        [Required]
        public DateTime BirthDate { get; set; }

        public int? Gender { get; set; }

        /// <summary>
        /// Inverse Prop.
        /// </summary>           
        private ICollection<PatientJob> _jobs;
        private ICollection<Visit> _visits;              

        public virtual ICollection<PatientJob> Jobs
        {
            get
            {
                if (_jobs == null)
                    this._jobs = new List<PatientJob>();
                return _jobs;
            }

            set => _jobs = value;
        }

        public virtual ICollection<Visit> Visits
        {
            get
            {
                if (_visits == null)
                    this._visits = new List<Visit>();
                return _visits;
            }
            set => _visits = value;
        }

        //Concurrency
        public byte[] RowVersion { get; set; }
    }

I use following automapper configuration

    var mapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile<PatientProfile>();
        });
        mapperConfiguration.AssertConfigurationIsValid();
        _mapper = mapperConfiguration.CreateMapper();

The Mapping profile as following

public class PatientProfile : Profile
{
    public PatientProfile()
    {
        this.CreateMap<Patient, PatientModel>(MemberList.Destination)
            .ForMember(pm => pm.LastVisitDate, opt => opt.MapFrom(p => p.Visits.AsEnumerable()
                .OrderByDescending(f => f.Id).Select(v => v.VisitDate)
                .FirstOrDefault()))
            .ForMember(pm => pm.Job,
                opt => opt.MapFrom(p =>
                    p.Jobs.AsEnumerable().OrderByDescending(f => f.Id).Select(a => a.Job).FirstOrDefault()));

        this.CreateMap<PatientModel, Patient>(MemberList.Destination)
            .ForMember(p => p.Id, opt => opt.Ignore())
            .ForMember(p => p.Jobs, opt => opt.Ignore())
            .ForMember(p => p.Visits, opt=>opt.Ignore())
            .ForMember(p=>p.RowVersion, opt=>opt.Ignore());
    }

I'm getting following exception

System.NullReferenceException

When I tried to map using following code:

        PatientModel pm = new PatientModel();
        pm.FirstName = "Anas";
        pm.LastName = "Tina";
        pm.BirthDate = new DateTime(1990, 1, 1);

        var patient = _mapper.Map<Patient>(pm);

I searched the web, downloaded the AutoMapper documentation with no luck.

Anas Tina
  • 329
  • 1
  • 2
  • 14

0 Answers0