asp.net-mvc - 复合主键与来自两个不同表mvc的两个外键

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

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

我的模型如下所示: 学生.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之间的多对多关系 讲师。因此,StudentLecturer 不能拥有单个项目 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/

相关文章:

c# - 绑定(bind) UI 元素不通过 Itemsource 中的更改值刷新

model-view-controller - Doctrine2(实体与存储库)

Spring MVC,捕获@ModelAttribute等对象列表

asp.net-mvc - 在ASP.Net MVC中定义自定义URL路由

c# - 尝试编辑不存在的项目的最佳实践?

asp.net-mvc - IIS7 上的 MVC 4 网站 - 因细微更改而中断(AutoMapper?)

C++抽象用户界面设计

jquery - ASP.NET MVC 验证属性和 Jquery

c# - LINQ IQueryable 使用 skip 和 take 返回相同的行

c# - Entity Framework 访问数据库方法