2017-10-07 24 views
2

我在讀這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; } 
} 

所以一個作者有很多書。許多書籍有一位作者(我知道這不是真正的生活,但僅用於教育目的)。

這是如何工作的?爲什麼一個年級有一批學生?

回答

3

相信的混亂歸結爲一個事實,即Grade代表education level(大學即第二年),而不是等級,如用於grading an assignment數字/字母。

+0

哇,。好吧,它可能是。我在我的例子中是否正確地做了這件事? – alex3wielki

+0

@ alex3wielki是的,我認爲你的代碼是正確的。 – orhtej2

+0

好的,謝謝。現在有道理 – alex3wielki