Mocanu Gabriel
Mocanu Gabriel

Reputation: 590

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

Upvotes: 1

Views: 72

Answers (1)

SBFrancies
SBFrancies

Reputation: 4230

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.

Upvotes: 1

Related Questions