简体   繁体   中英

Convetion of relation table with Entity Framework Core

I read the Entity Framework Core documentation on how to customize one-to-many relationship and I don't understand the difference between this convention. You can see these convention here [1].

So what it's the difference between these conventions ?

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

I tried only the conventions 1 and 2.

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

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

In the first example you access the Grade property through the Student entity to see a student's grade. In the second example you would access the Students property through the Grade entity to see all students with that grade. You can actually do both and in each instance you can define the relationship specifically using the fluent API or relevant attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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