[英]Confusing One-to-Many Entity Framework example
我正在阅读本教程 。 我先对代码进行一对多的处理。 示例是:
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; }
}
因此,一位作者有很多书。 许多书都有一位作者(我知道这不是现实生活,只是出于教育目的)。
这是如何运作的? 为什么一个年级有一批学生?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.