2017-10-20 68 views
0

我有3個表無法獲取類的屬性(ASP.NET MVC)

預約,醫生和Appointment_to_Doctor

這裏是約會類

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public Appointment() 
    { 
     this.Patient_to_appointment = new HashSet<Patient_to_appointment>(); 
     this.Appointments_to_Doctors = new HashSet<Appointments_to_Doctors>(); 
    } 

    [Key] 
    public int Id { get; set; } 
    public string Start_appointment { get; set; } 
    public string End_appointment { get; set; } 
    public string Title { get; set; } 
    public string Type_of_appointment { get; set; } 
    [ForeignKey("Patient")] 
    public Nullable<int> Patient_id { get; set; } 
    public string Kasse { get; set; } 
    public Nullable<System.DateTime> Date { get; set; } 

    public virtual Patient Patient { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Patient_to_appointment> Patient_to_appointment { get; set; } 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Appointments_to_Doctors> Appointments_to_Doctors { get; set; } 
} 

這裏是醫生類

public partial class Doctor 
{ 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
    public Doctor() 
    { 
     this.Appointments_to_Doctors = new HashSet<Appointments_to_Doctors>(); 
    } 
    [Key] 
    public int Id { get; set; } 
    public string Organization { get; set; } 
    public string Title { get; set; } 
    public string Sex { get; set; } 
    public string First_Name { get; set; } 
    public string Last_Name { get; set; } 
    public string C_O { get; set; } 
    public string Street { get; set; } 
    public string Index { get; set; } 
    public string City { get; set; } 
    public string Country { get; set; } 
    public string Email { get; set; } 
    public string Phone { get; set; } 
    public string Fax { get; set; } 

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
    public virtual ICollection<Appointments_to_Doctors> Appointments_to_Doctors { get; set; } 
} 

and third class

public partial class Appointments_to_Doctors 
{ 
    [Key] 
    public int Id { get; set; } 
    public Nullable<int> Doctor_id { get; set; } 
    public Nullable<int> Appointment_id { get; set; } 
    [ForeignKey("Appointment_id")] 
    public virtual Appointment Appointment { get; set; } 
    [ForeignKey("Doctor_id")] 
    public virtual Doctor Doctor { get; set; } 
} 

我需要從前面的select中獲取醫生的id,將它傳遞給後端並進行選擇。

所以在後端我有這個代碼。

public JsonResult GetEvents() 
    { 
     using (var ctx = new ApplicationDbContext()) 
     { 
      var eventList = ctx.Appointments.Where(e=> e.Appointments_to_Doctors.).Select(e => new 
      { 
       id = e.Id, 
       title = e.Title, 
       start = e.Start_appointment.ToString(), 
       end = e.End_appointment.ToString(), 
       allDay = false 
      }); 
      var rows = eventList.ToArray(); 
      return Json(rows, JsonRequestBehavior.AllowGet); 
     } 
    } 

但這裏ctx.Appointments.Where(e=> e.Appointments_to_Doctors.)點後,我不能寫Doctor_id。爲什麼?

我有這個

嚴重性代碼說明項目文件的線路抑制狀態 錯誤CS1061「ICollection的」不包含「Doctor_id」的定義,並沒有擴展方法「Doctor_id」接受一個類型的第一個參數「 ICollection的」可以找到(是否缺少using指令或程序集引用?)RS_Main C:\用戶\ nemes \來源\回購\ RIS_Project_New \ RS_Main \ \控制器105 CalendarController.cs主動

感謝的求助非常!

+0

你是什麼意思,你不能寫?智能感知不會給你選擇,或者你輸入'Doctor_id'並給你編譯錯誤? – FortyTwo

+0

我更新了我的問題@FortyTwo –

+0

爲什麼你的模型類部分類?如果你在'Appointments_to_Doctors'表中查看你的數據庫,你有一個名爲'Doctor_id'的列嗎?連接表有可空的外鍵也有點奇怪 - 這看起來像是一個錯誤 – GregH

回答

0

但這裏ctx.Appointments.Where(E => e.Appointments_to_Doctors),我不能寫Doctor_id。爲什麼?

這是因爲在你Appointments類聲明該財產Appointments_to_Doctors作爲收藏品,這意味着除非您是否要像.Where,或.Any執行某種類型的方法,你將無法訪問個人財產成員。等等這些屬性。

您需要更改到:

ctx.Appointments_to_Doctors.Where(e=> e.Doctor_Id.Value == yourValue).Select(e => new 
    { 
     id = /* your value */, 
     title = /* your value */, 
     start = /* your value */, 
     end = /* your value */, 
     allDay = false 
    }); 

見,這條線執行對Doctor_Id.Value.Where方法。

讓我知道這是否有幫助!

+0

它有幫助。謝謝 –

+0

不客氣。快樂的編碼! –

0

我有類似的代碼,在我正在工作的應用程序中爲我工作,嘗試在醫生表的約會中添加此公共int Doctor_id,對於同一表中的約會也是如此。

基本上,我認爲,你需要存儲的ID爲約會和小數點後醫生

+0

你正在導入醫生的班級,但沒有鏈接,所以你需要的ID,同樣會發生預約。 –

+0

也,我不知道你想要達到什麼,但首先檢查醫生的約會,然後醫生,而不是其他方式 –