我有以下数据库:
我已经从这个数据库中创建了 EDMX 模型。
一切都很好,直到我将Diagnosis
表格添加到图表中。
下面的查询开始出错:
var appointments = from a in db.Appointment.Include("Patient")
where a.DoctorID == doctorId
select a;
我开始收到以下错误:
列名“DiagnosisID”无效。
在调试模式下,我检查了生成的数据库查询,它是:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[DoctorID] AS [DoctorID],
[Extent1].[PatientID] AS [PatientID],
[Extent1].[DiagnosisID] AS [DiagnosisID],
[Extent1].[Date] AS [Date],
[Extent1].[Time] AS [Time],
[Extent1].[Duration] AS [Duration],
[Extent1].[Notes] AS [Notes],
[Extent2].[ID] AS [ID1],
[Extent2].[ContactDetailID] AS [ContactDetailID],
[Extent2].[FirstName] AS [FirstName],
[Extent2].[LastName] AS [LastName],
[Extent2].[Sex] AS [Sex],
[Extent2].[BirthDate] AS [BirthDate],
[Extent2].[CurrentDate] AS [CurrentDate],
[Extent2].[Notes] AS [Notes1]
FROM [dbo].[Appointment] AS [Extent1]
LEFT OUTER JOIN [dbo].[Patient] AS [Extent2] ON [Extent1].[PatientID] = [Extent2].[ID]
WHERE (([Extent1].[DoctorID] = @p__linq__0) AND ( NOT ([Extent1].[DoctorID] IS NULL OR @p__linq__0 IS NULL))) OR (([Extent1].[DoctorID] IS NULL) AND (@p__linq__0 IS NULL))
/*
Int32 p__linq__0 = 1
*/
我的设计有什么问题吗?
以下是约会表设计:
这是约会表的 FK 定义:
一旦我调用可枚举对象的ToList()方法,就会发生异常,如下所示:
EF生成的类如下:
public partial class Appointment
{
public int ID { get; set; }
public Nullable<int> DoctorID { get; set; }
public Nullable<int> PatientID { get; set; }
public Nullable<int> DiagnosisID { get; set; }
public Nullable<System.DateTime> Date { get; set; }
public Nullable<System.DateTime> Time { get; set; }
public Nullable<byte> Duration { get; set; }
public string Notes { get; set; }
public virtual Diagnosis Diagnosis { get; set; }
public virtual Doctor Doctor { get; set; }
public virtual Patient Patient { get; set; }
}
public partial class Diagnosis
{
public Diagnosis()
{
this.Appointment = new HashSet<Appointment>();
}
public int ID { get; set; }
public string Symptoms { get; set; }
public string Treatment { get; set; }
public Nullable<System.DateTime> CurrentDate { get; set; }
public string Notes { get; set; }
public virtual ICollection<Appointment> Appointment { get; set; }
}
EDMX 生成的类如下: