table 与 Entity Framework 核心关系的惯例

Convetion of relation table with Entity Framework Core

我阅读了 Entity Framework 有关如何自定义一对多关系的核心文档,但我不明白此约定之间的区别。您可以在此处查看这些约定 [1].

那么这些约定之间有什么区别?

[1]:https://www.entityframeworktutorial.net/efcore/one-to-many-conventions-entity-framework-core.aspx

我只尝试了约定 1 和 2。

约定 1:

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; }
}

约定 2:

public class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
}

public class Grade
{
    public int GradeId { get; set; }
    public string GradeName { get; set; }
    public string Section { get; set; }

    public ICollection<Student> Students { get; set; } 
}

在第一个示例中,您通过 Student 实体访问 Grade 属性 以查看学生的成绩。在第二个示例中,您将通过 Grade 实体访问 Students 属性 以查看具有该成绩的所有学生。实际上,您可以同时执行这两种操作,并且在每种情况下,您都可以使用流畅的 API 或相关属性来专门定义关系。