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

时间:2016-06-21 16:27:09

标签: asp.net-mvc entity-framework model-view-controller orm composite-primary-key

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

我的模特看起来像这样: Student.cs

lynx localhost:80

Lecturer.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; }
}

ProposalFB.cs

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

真的很感激如何纠正这个

的一些指导

1 个答案:

答案 0 :(得分:2)

您的ProposalFB实体代表many-to-manyStudent 之间的Lecturer关系。因此,StudentLecturer不能包含单个项ProposalFB属性,它应该是ProposalFB的集合:

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

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