Newbie
Newbie

Reputation: 61

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

I am fairly new to ASP.NET MVC, I have tried to find a solution for this but I am unable to find a similar example, I have read that this is because of my relationship being null when it has to be *. I want my ProposalFB table to have a composite primary key of a lecturer email(lecEmail) and a student email(studEmail) both coming from different tables(Lecturer and Student), at first, I thought I could fix it if there was data in the lecturer and student table unfortunately that did not work. Whenever I try to scaffold I get enter image description here

My models look like this: 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; }
}

Really appreciate some guidance to how to correct this

Upvotes: 2

Views: 1877

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205589

Your ProposalFB entity represents many-to-many relationship between Student and Lecturer. Hence Student and Lecturer cannot have a single item ProposalFB property, it should be a collection of ProposalFB:

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

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

Upvotes: 3

Related Questions