[英]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.