我对 ASP.NET MVC 相当陌生,我试图找到解决方案,但我找不到类似的示例,我读到这是因为我的关系在必须为 * 时为空。我希望我的 ProposalFB 表有一个来自不同表(讲师和学生)的讲师电子邮件(lecEmail)和学生电子邮件(studEmail)的复合主键,起初,我认为如果有数据,我可以修复它不幸的是,讲师和学生 table 不起作用。每当我尝试搭建脚手架时,我都会得到
我的模型如下所示: 学生.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; }
}
讲师.cs
public class Lecturer
{
[Key]
public string lecEmail { get; set; }
public virtual ProposalFB ProposalFB { get; set; }
}
提案FB.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之间的
和多对多
关系
讲师
。因此,Student
和 Lecturer
不能拥有单个项目 ProposalFB
属性,它应该是 ProposalFB
的集合:
public class Student
{
// ...
public virtual ICollection<ProposalFB> ProposalFB { get; set; }
}
public class Lecturer
{
// ...
public virtual ICollection<ProposalFB> ProposalFB { get; set; }
}
关于asp.net-mvc - 复合主键与来自两个不同表mvc的两个外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37949819/