2016-08-27 13 views
0

で仕事何ん:次のように1対多のrelationship.Classesが含まれ、患者と医師は、次のとおりです:ナビゲーションは、私は私のデータベース内の2つのエンティティ持っているC#の

public partial class Doctor 
{ 
    public Doctor() 
    { 
     this.Patients = new HashSet<Patient>(); 
    } 
    public int DoctorID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Email { get; set; } 
    public string Country { get; set; } 
    public System.DateTime Birthday { get; set; } 
    public byte[] Photo { get; set; } 
    public string Password { get; set; } 
    public string PasswordSalt { get; set; } 
    public int SpecialityID { get; set; } 

    public virtual Speciality Speciality { get; set; } 
    public virtual ICollection<Patient> Patients { get; set; } 
} 



public partial class Patient 
{ 
    public int PatientID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Email { get; set; } 
    public string Gender { get; set; } 
    public string MaritalStatus { get; set; } 
    public System.DateTime Birthday { get; set; } 
    public string Phone { get; set; } 
    public int DoctorID { get; set; } 
    public System.DateTime EntryDate { get; set; } 

    public virtual Doctor Doctor { get; set; } 
    public virtual PatientAddress PatientAddress { get; set; } 
} 

これはコードです患者を医者に追加するためのものである。データベース内で患者を追加

public ActionResult AddPatient(PatientViewModel patientVM) 
    { 
     using (PeopleCareEntities PeopleEntities=new PeopleCareEntities()) 
     { 
      PatientAddress patientAddress = Mapper.Map<PatientViewModel, PatientAddress>(patientVM); 
      Patient patient = Mapper.Map<PatientViewModel, Patient>(patientVM); 
      int currentDoctor = ((Doctor)Session["Doctor"]).DoctorID; 
      //problem is here 
      Doctor doctor=PeopleEntities.Doctors.Single(a=>a.DoctorID==currentDoctor); 
      var doctorPatients = doctor.Patients.FirstOrDefault(a=>a.Email==patientVM.Email); 
      if (doctorPatients==null) 
      { 
       patient.EntryDate = DateTime.Now; 
       patient.DoctorID = doctor.DoctorID; 
       doctor.Patients.Add(patient); 
       PeopleEntities.SaveChanges(); 
       patientAddress.PatientID = patient.PatientID; 
       PeopleEntities.PatientAddresses.Add(patientAddress); 
       PeopleEntities.SaveChanges(); 
       return Json(new { Message = "Patient added successfully !" }, JsonRequestBehavior.AllowGet); 
      } 
      else 
      { 
       return Json(new { Message="Patient already exist !" }, JsonRequestBehavior.AllowGet); 
      } 
     } 
    } 

は完璧に動作しますが、デバッグモードでdoctor.Patients has always Count=0.。 ありがとうございます。

答えて

1

ドクターエンティティは、この試すロード:

Doctor doctor=PeopleEntities.Doctors.Single(a => a.DoctorID == currentDoctor) 
            .Include(a => a.Patients); 
関連する問題