使用自动映射器进行映射时出现Stackoverflow异常

时间:2019-07-07 07:21:22

标签: c# .net entity-framework automapper

我在使用自动映射器传递对象时遇到了StackOverflow异常。

This is before mapping getting result accurately Here is the error where exception comes when mapping 我的域模型类是

public class Appointment
    {
        [Key]
        public int AppointmentID { get; set; }
        public int Serial { get; set; }
        public int AppointmentTypeID { get; set; } = 2;
        public DateTime CreatedAt { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? UpdatedAt { get; set; }
        public string UpdatedBy { get; set; }

        [ForeignKey("CreatedBy")]
        public virtual User User { get; set; }
        public virtual ICollection<VisitRequest> VisitRequests { get; set; }
        [ForeignKey("AppointmentTypeID")]
        public virtual VisitPurpose VisitPurpose { get; set; }
        public virtual ICollection<AppointmentStatus> AppointmentStatuses { get; set; }
    }

我的视图模型类是

 public class ApproveAppointmentViewModel
    {
        [Key]
        public int AppointmentID { get; set; }
        public int Serial { get; set; }
        public int AppointmentTypeID { get; set; }
        public virtual ICollection<ApproveAppointmentVisitRequestViewModel> VisitRequests { get; set; }
        public virtual ICollection<ApproveAppointmentStatusViewModel> AppointmentStatuses { get; set; }
    }
    public class ApproveAppointmentVisitRequestViewModel
    {
        [Key]
        public int VisitRequestID { get; set; }
        public bool SendSMS { get; set; }
        public int? PurposeID { get; set; }
        public string PurposeDescription { get; set; }
        public string Attachment { get; set; }
        public int VisitorID { get; set; }
        public int? AppointmentID { get; set; }

        public virtual ApproveAppointmentViewModel Appointment { get; set; }
        public virtual ApproveAppointmentVisitorViewModel Visitor { get; set; }
    }
    public class ApproveAppointmentVisitorViewModel
    {
        [Key]
        public int? VisitorID { get; set; }
        public string NationalID { get; set; }
        public string FullName { get; set; }
        public int Gender { get; set; }
        public string Job { get; set; }
        public string Type { get; set; }
        public string PhoneNumber { get; set; }
        public string Email { get; set; }
        public virtual ICollection<ApproveAppointmentVisitRequestViewModel> VisitRequests { get; set; }
    }
    public class ApproveAppointmentStatusViewModel
    {
        public int StatusID { get; set; }
        public int AppointmentID { get; set; }
        public int StatusCode
        {
            get
            {
                return 1;
            }
        }
        public DateTime ValidFrom
        {
            get
            {
                return DateTime.UtcNow.AddHours(3);
            }
        }
        [UnavailableDate]
        public DateTime? AppointmentDateTime
        {
            get
            {
                if (AppointmentDate != null)
                {
                    if (AppointmentTime != null && AppointmentTime.Length > 0)
                        return new DateTime(AppointmentDate.Value.Year, AppointmentDate.Value.Month, AppointmentDate.Value.Day, int.Parse(AppointmentTime.Split(':')[0]), int.Parse(AppointmentTime.Split(':')[1]), 0);
                    else
                        return new DateTime(AppointmentDate.Value.Year, AppointmentDate.Value.Month, AppointmentDate.Value.Day);
                }
                else
                {
                    return null;
                }
            }
        }
        public DateTime? AppointmentDate { get; set; }
        public DateTime? _AppointmentDate { get; set; }
        [Display(Name = "التاريخ")]
        public string AppointmentDateHijri
        {
            get
            {
                if (AppointmentDate.HasValue)
                    return AppointmentDate.Value.ConvertToHijriString();
                return null;
            }
            set
            {
                if (value != null)
                {
                    var generalDate = new DateTime(int.Parse(value.Split('/')[0]), int.Parse(value.Split('/')[1]), int.Parse(value.Split('/')[2]));
                    AppointmentDate = generalDate.Year > 1500 ? generalDate : generalDate.ConvertToGeorgian();
                }
                else
                    AppointmentDate = null;
            }
        }
        [Display(Name = "ملاحظة الإعتماد")]
        public string StatusNote { get; set; }
        [Required(ErrorMessage = "يجب تحديد الوقت المعتمد")]
        [Display(Name = "الوقت")]
        public string AppointmentTime { get; set; }
        [Range(0, 60, ErrorMessage = "أقل مدة للزيارة 10 دقائق واقصى مدة 60 أو مفتوح")]
        [Display(Name = "المدة (دقائق)")]
        public int? AppointmentDuration { get; set; }
        public virtual ApproveAppointmentViewModel Appointment { get; set; }
    }

控制器代码为

var AppointmentRequest = db.Appointments.Where(p => p.AppointmentID == id).Include(m => m.VisitRequests).Include(m => m.AppointmentStatuses).FirstOrDefault();

在这里,使用自动映射器映射时出现异常

var vmodel = Mapper.Map<ApproveAppointmentViewModel>(AppointmentRequest);

0 个答案:

没有答案