将源中具有项目的对象映射到目标中的项目列表,以及目标中的RowVersion

时间:2018-10-30 00:20:27

标签: c# entity-framework automapper-5

我有几个小时试图在以下类型之间进行映射:

源类型:

    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; }
    }

目的地类型:

    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; }
    }

我使用以下自动映射器配置

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

如下所示的映射配置文件

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());
    }

我正在跟踪异常

  

System.NullReferenceException

当我尝试使用以下代码进行映射时:

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

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

我在网上搜索,没有运气就下载了AutoMapper文档。

0 个答案:

没有答案