繁体   English   中英

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

[英]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; }
}

因此,一位作者有很多书。 许多书都有一位作者(我知道这不是现实生活,只是出于教育目的)。

这是如何运作的? 为什么一个年级有一批学生?

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

暂无
暂无

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

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