Confusing One-to-Many Entity Framework example

Viewed 36

I'm reading this tutorial. I'm doing one to many with code first. The example is:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Grade Grade { get; set; }
}

public class Grade
{
    public int GradeID { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }

    public ICollection<Student> Student { get; set; }//Why this?
}

But to me, it logically makes no sense. Why is there a collection of students in grade? Shouldn't it be the other way around? My own example is this

public class Author
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    [Display(Name = "Date of birth")]
    public DateTime DateOfBirth { get; set; }

    public ICollection<Book> Books { get; set; }
}

public class Book
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string Title { get; set; }

    [Display(Name = "Publication Name")]
    public DateTime PublicationDate { get; set; }

    [Required]
    public int Edition { get; set; }

    [Required]
    public Author Author { get; set; }
}

So one author has many books. And many books have one author (I know it's not real life, but just for educational purposes).

How does this work? Why does one grade have a collection of students?

1 Answers

I believe the confusion comes down to the fact that Grade represents education level (i.e. second year of university) rather than grade as in number/letter used for grading an assignment.

Related