我有以下数据库:
我已经从这个数据库中创建了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定义:
一旦我调用了enumarable对象的 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生成的类如下:
答案 0 :(得分:0)
我刚发现了这个问题。它在连接字符串中! 我忘记更改连接字符串,因为它指向生产服务器,并且我没有将数据库更改回生产数据库。
谢谢你们的努力。