1

我是相當新的ASP.NET MVC,我試圖找到一個解決方案,但我無法找到一個類似的例子,我讀過這是因爲當它必須是*時,我的關係是空的。我希望我的ProposalFB表具有來自不同表格(講師和學生)的講師電子郵件(leeEmail)和學生電子郵件(studEmail)的組合主鍵,起初,我認爲如果有數據講師和學生表不幸的是沒有工作。每當我嘗試腳手架,我得到 enter image description here複合主鍵與兩個不同的表中的兩個外鍵mvc

我的模式是這樣的: Student.cs

public class Student 
{ 
    [Key] 
    public string studEmail { get; set; } 
    public string projectType { get; set; } 
    public string projectTitle { get; set; } 
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] 
    public Nullable<System.DateTime> year { get; set; } 
    public virtual ProposalFB ProposalFB { get; set; } 
} 

Lecturer.cs

public class Lecturer 
{ 
    [Key] 
    public string lecEmail { get; set; } 
    public virtual ProposalFB ProposalFB { get; set; } 
} 

ProposalFB.cs

public class ProposalFB 
{ 
    [Key, ForeignKey("Student"), Column(Order = 0)] 
    public string studEmail { get; set; } 
    [Key, ForeignKey("Lecturer"), Column(Order = 1)] 
    public string lecEmail { get; set; } 
    public string feedback1 { get; set; } 
    public string feedback2 { get; set; } 
    public string feedback3 { get; set; } 
    public string feedback4 { get; set; } 
    public string feedback5 { get; set; } 
    public float proposalMark { get; set; } 
    public Nullable<System.DateTime> createdOn { get; set; } 
    public Nullable<System.DateTime> modified { get; set; } 
    public bool status { get; set; } 
    public virtual Student Student { get; set; } 
    public virtual Lecturer Lecturer { get; set; } 
} 

真正感謝一些指導如何糾正

+2

1.組合鍵通常是一個壞主意。 2.由外鍵組成的複合鍵總是*一個壞主意。 3.鍵(主鍵或外鍵)應該放在索引列上,這些鍵不太適合。長與短:只需在桌面上添加一些好的身份主鍵,並停止所有這些愚蠢行爲。 –

+0

@ChrisPratt我這樣做的理由是爲了跟蹤學生和講師的電子郵件地址在「ProposalFB」表中不再重複,是否有一種比我提出的模型更容易管理的方法? – Newbie

+0

當然。只需在列上添加一個唯一的約束。不需要成爲關鍵。 –

回答

1

您的ProposalFB實體代表Student Lecturer之間的關係many-to-many。因此StudentLecturer不能有一個單一的項目ProposalFB屬性,它應該是ProposalFB集合:

public class Student 
{ 
    // ... 
    public virtual ICollection<ProposalFB> ProposalFB { get; set; } 
} 

public class Lecturer 
{ 
    // ... 
    public virtual ICollection<ProposalFB> ProposalFB { get; set; } 
} 
+0

這按預期工作,並糾正了我所遇到的問題 – Newbie

相關問題