SQLite与Entity Framework搭建医患预约一对多关联咨询
现有代码语法修正
先调整你原有代码中不符合C#语法规范的部分:
- 属性名不能包含空格,
Full name改为FullName,Patient ID改为PatientId - 泛型类型名大小写要对应,
new List<appointment>()改为new List<Appointment>()
一、新增/修改实体代码
1. 新增Doctor类
实现与预约、就诊记录的双向关联:
public class Doctor { public int Id { get; set; } // 医生姓名,后续用作筛选条件 public string Name { get; set; } // 可选字段:科室,方便创建预约时分类选择医生 public string Department { get; set; } // 双向关联:一个医生对应多个预约 public IList<Appointment> Appointments { get; set; } = new List<Appointment>(); // 后续上线就诊记录模块时生效的导航属性 public IList<Attendance> Attendances { get; set; } = new List<Attendance>(); }
2. 修改Appointment类
补充医生关联的外键和导航属性:
public class Appointment { public int Id { get; set; } public DateTime CreatedOn { get; set; } // 建议新增预约时间字段,后续按日期筛选预约会用到 public DateTime AppointmentTime { get; set; } public string Comment { get; set; } // 原有患者关联 public int PatientId { get; set; } public Patient Patient { get; set; } // 新增医生关联 public int DoctorId { get; set; } public Doctor Doctor { get; set; } }
3. 修正后的Patient类
public class Patient { public int Id { get; set; } public string FullName { get; set; } public IList<Appointment> Appointments { get; set;} = new List<Appointment>(); // 后续上线就诊记录模块时生效的导航属性 public IList<Attendance> Attendances { get; set; } = new List<Attendance>(); }
二、业务逻辑实现说明
- 创建预约关联医生:创建预约时,只需要给
Appointment.DoctorId赋值为选中的医生ID即可完成关联,无需手动操作导航属性。 - 按医生姓名、日期筛选预约(以EF Core为例):
// 筛选姓名为"李医生"的医生2024年6月的所有预约 var doctorAppointments = dbContext.Appointments .Include(a => a.Doctor) .Include(a => a.Patient) .Where(a => a.Doctor.Name == "李医生" && a.AppointmentTime.Year == 2024 && a.AppointmentTime.Month == 6) .ToList();
三、后续就诊记录模块扩展参考
后续上线Attendance模块时,实体定义参考:
public class Attendance { public int Id { get; set; } public DateTime VisitTime { get; set; } public string Remark { get; set; } // 医生填写的就诊备注 // 关联患者 public int PatientId { get; set; } public Patient Patient { get; set; } // 关联医生 public int DoctorId { get; set; } public Doctor Doctor { get; set; } }
内容的提问来源于stack exchange,提问作者user450157
相关产品推荐
相关产品推荐

