简体   繁体   English

令人困惑的一对多实体框架示例

[英]Confusing One-to-Many Entity Framework example

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? 为什么一个年级有一批学生?

我相信,混乱归结到这一事实Grade代表教育水平 (即大学的第二年),而不是等级,如用于数字/字母等级分配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM