2017-10-20 101 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

也,我不知道你想要达到什么,但首先检查医生的约会,然后医生,而不是其他方式 –

相关问题