2013-08-17 156 views
4

我有以下數據庫:實體框架5.0奇怪的問題

enter image description here

我已經創建了EDMX模型出這個數據庫。

一切是偉大的工作,直到我說的Diagnosis表圖的時刻。

下面的查詢開始犯錯誤:

var appointments = from a in db.Appointment.Include("Patient") 
        where a.DoctorID == doctorId 
        select a; 

我開始收到以下錯誤:

Invalid column name '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 
*/ 

任何問題在我的設計?

下面是預約表設計:

enter image description here

這裏是約會表FK定義:

enter image description here

發生異常,一旦我稱之爲ToList( )所述enumarable對象的方法如下:

enter image description here

的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生成的類是如下:

enter image description here

+2

你看着你的SQL數據庫,如果一切正常與預約表? – kkocabiyik

+0

是'DiagnosisID'字段名稱與數據庫中約會表中的列名完全匹配嗎? –

+0

是的,它們確實與數據庫中的字段名稱「DiagnosisID」匹配。 – ZooZ

回答

0

我剛發現的問題。它在連接字符串中! 我忘記更改連接字符串,因爲它指向生產服務器,並且我沒有將數據庫同步更改回生產數據庫。

感謝你們的努力。