无法获取类的属性(ASP.NET MVC)

时间:2017-10-20 13:46:28

标签: c# asp.net asp.net-mvc

我有3张桌子

预约,医生和预约_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; }
}

和第三类

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

我需要从前面的选择中获取医生的身份,将其传递到后端并进行选择。

所以在后端我有这个代码。

 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&#39; ICollection&#39;不包含&#39; Doctor_id&#39;的定义没有扩展方法&#39; Doctor_id&#39;接受类型&#39; ICollection&#39;的第一个参数。可以找到(您是否缺少using指令或程序集引用?)RS_Main C:\ Users \ nemes \ Source \ Repos \ RIS_Project_New \ RS_Main \ Controllers \ CalendarController.cs 105 Active

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

我有类似的代码在我正在处理的应用程序中适用于我,尝试在医生表的约会中添加此public int Doctor_id,同样用于同一表中的约会。

基本上,我认为您需要存储约会和医生的ID

答案 1 :(得分:0)

但是这里的ctx.Appointments.Where(e =&gt; 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
    });

请参阅此行,在.Where上执行Doctor_Id.Value方法。

请告诉我这是否有帮助!