具有来自两个不同表 mvc 的两个外键的复合主键

Composite primary key with two foreign keys from two different tables mvc

我是 ASP.NET MVC 的新手,我试图为此找到解决方案,但找不到类似的示例,我读到这是因为我的关系在它时为空必须 *。我希望我的 ProposalFB table 具有来自不同 tables(讲师和学生)的讲师电子邮件(lecEmail)和学生电子邮件(studEmail)的复合主键,起初,我想如果讲师和学生中有数据,我可以修复它 table 不幸的是,这不起作用。每当我尝试搭建脚手架时,我都会得到

我的模型是这样的: 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; }
}

非常感谢有关如何更正此问题的一些指导

您的 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; }
}