2

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

Newbie
  • 61
  • 3
  • 10
  • 2
    1. Composite keys are generally a bad idea. 2. Composite keys composed of foreign keys are *always* a bad idea. 3. A key (primary or foreign) should be on an indexed column, which strings are not overly suited towards. Long and short: just add some good identity primary keys to your table and stop all this silliness. – Chris Pratt Jun 21 '16 at 16:38
  • @ChrisPratt my reason for doing this is to keep track of a student and lecturers email address not being repeated in the "ProposalFB" table, is there an easier way to manage this than my proposed model? – Newbie Jun 21 '16 at 16:46
  • Sure. Just add a unique constraint on the column. Doesn't need to be a key. – Chris Pratt Jun 21 '16 at 16:48
  • @ChrisPratt not the column will only have one unique value, but the combination of both the lecturer and student email will not be repeated, the lecturer email may only map to a student email only once and vice versa if that is any clearer? – Newbie Jun 21 '16 at 16:53
  • See: http://stackoverflow.com/questions/18889218/unique-key-constraints-for-multiple-columns-in-entity-framework#23090070 – Chris Pratt Jun 21 '16 at 17:11

1 Answers1

3

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; }
}
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343