简体   繁体   中英

Many to Many Relationship Representation in EF Code First

I am trying to develop a structure in the following manner...

  • I am representing Courses which are taught by Teachers.
  • For each of those Courses there are any number of Exams (because the Exam is randomly generated, each generation counts as a different exam), and I have to track that in the database.
  • An Exam can be taken by 0 or more Students. (And, conversely, a Student can have taken one or more Exams.)
  • Once the Student takes the exam (which is done via a web interface that I have already developed), that Student receives a score for the Exam and an ExamStatus (Passed, Failed, Retake, TestOut, etc.)
  • There are some other "stuff" in there that I didn't address such as Exams are made up of Questions (which are created by the Teacher), etc, but I have a good understanding of that.

I have almost finished my structure, but I am struggling with how to represent the relationship of Students to Exams and also working the ExamStatus. Here is my thought so far. (It is late, so my apologies for stupid errors.)

public class Student
{
    public string StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // Should this even be part of the Student POCO?
    public virtual ICollection<Exam> ExamsTaken { get; set; }
}

// I know this is right - putting it here to provide all the classes in question.
public class ExamStatus
{
    public int ExamStatusID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}

// This is the POCO that really hangs me up.
public class Exam
{
    public int ExamID { get; set; }
    public int CourseID { get; set; }
    public string ExamTitle { get; set; }

    // Not sure if this should be part of Exam, or part of a different
    // object?
    public decimal? Score { get; set; }
    public int? ExamStatusID { get; set; }

    public virtual ICollection<Question> Questions { get; set; }
    public virtual Course Course { get; set; }
    public virtual ExamStatus ExamStatus { get; set; }
}

I would appreciate any help with this many-to-many relationship (Students <-> Exams). I think I've thought about it too much and have thoroughly confused myself on something that is probably fairly straightforward.

The exam is seperate to an exam result.

Hence pull out the scores and status to a seperate table. The status field may also want to include a taken or pending, so that you know they have taken it but it is awaiting marks (that is unless it is all done automatically)

That way a student has a list of exams taken, which is linked to the score table and the score table is linked to which exam it was.

This is what I would change. I don't THINK its exactly what Adam said. If it is let me know and i'll remove the post

public class Student
{
    public string StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    // changed this to results of the exams taken
    // you can still get exam via ExamResult -> Exam
    public virtual ICollection<ExamResult> ExamsTaken { get; set; }
}

public class Exam
{
    public int ExamID { get; set; }
    public int CourseID { get; set; }
    public string ExamTitle { get; set; }

    public virtual ICollection<Question> Questions { get; set; }
    public virtual Course Course { get; set; }
    // used to be ICollection of exam statuses, but that got moved to the
    // ExamResult class
    public virtual ICollection<ExamResults> Results { get; set; }
}

public class ExamResult
{
    public int ExamID { get; set; }
    public int StudentID { get; set; }

    public decimal? Score { get; set; }
    // all your results should have a status right? im assuming
    // unfinished or not started exams have statuses
    // so this shouldn't be nullable
    public int ExamStatusID { get; set; }

    public virtual Student Student { get; set; }
    public virtual Exam Exam { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM