我正在阅读tutorial。我先用代码做一对多。例子是:
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?
}
但对我来说,它在逻辑上毫无意义。为什么有成绩的学生?不应该反过来吗?我自己的例子就是这个
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; }
}
所以一位作者有很多书。很多书都有一位作者(我知道这不是现实生活,只是出于教育目的)。
这是如何工作的?为什么一个年级有学生集合?