【问题标题】:Cannot get property of class(ASP.NET MVC)无法获取类的属性(ASP.NET MVC)
【发布时间】:2017-10-20 22:12:36
【问题描述】:

我有 3 张桌子

约会、医生和 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; }
}

三等舱

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

我需要从前面的选择中获取医生的 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=&gt; e.Appointments_to_Doctors.) 在点之后,我不能写Doctor_id。为什么?

我有这个

严重性代码描述项目文件行抑制状态 错误 CS1061“ICollection”不包含“Doctor_id”的定义,并且找不到接受“ICollection”类型的第一个参数的扩展方法“Doctor_id”(您是否缺少 using 指令或程序集引用?) RS_Main C:\ Users\nemes\Source\Repos\RIS_Project_New\RS_Main\Controllers\CalendarController.cs 105 活动

非常感谢您的帮助!

【问题讨论】:

  • 你不会写是什么意思? IntelliSense 不给你选项或者你输入Doctor_id 并给你编译错误?
  • 我更新了我的问题@FortyTwo
  • 为什么你的模型类是部分类?如果您在Appointments_to_Doctors 表中查看您的数据库,您是否有一个名为Doctor_id 的列?链接表具有可为空的外键也有点奇怪 - 这似乎是一个错误
  • 看来你的问题是你有一个 iccollection 并且你的属性存在于一个实例上。试试:e=> e.Appointments_to_Doctors.Any(atd => atd.Doctor_id == {yourid}) 这对你有用吗?
  • 类是通过 EF 从 DB 生成的,是的,我在 DB@peggy 中有这个专栏

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


【解决方案1】:

但是在这里 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 方法。

如果这有帮助,请告诉我!

【讨论】:

  • 它有帮助。谢谢
【解决方案2】:

在我正在处理的应用程序中,我有类似的代码适用于我,尝试将这个公共 int Doctor_id 添加到医生表的约会中,同一张表中的约会相同。

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

【讨论】:

  • 您正在导入医生的课程,但未链接,因此您需要 id,约会也会发生同样的情况。
  • 另外,我不知道您要联系什么,但请先检查医生的预约,然后再检查医生,而不是反过来
猜你喜欢
  • 2017-10-15
  • 1970-01-01
  • 2016-06-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
  • 2019-05-14
  • 2015-06-14
  • 1970-01-01
相关资源
最近更新 更多