Should I define `foreign key Id` property in addition to the corresponding `navigation property`?

.net .net-core c# entity-framework entity-framework-core

Question

I found these entities in one of Microsoft tutorials:

public class Enrollment
{
    public int Id{ get; set; }

    public int StudentId { get; set; }
    public Student Student { get; set; }
}

public class Student
{
    public int Id { get; set; }

    public ICollection<Enrollment> Enrollments { get; set; }
}

Do I really need to define StudentId property?

How the case will differ in case of different types of relationshipts? for example one-one and one-many?

UPDATE:

In this link http://www.learnentityframeworkcore.com/relationships we can see that in One-Many and Many-Many, they did not define the Id foreign key property, but in One-One, they did. Why is that?

1
6
11/4/2017 12:41:24 AM

Accepted Answer

I found a partial answer for my question in this page:

http://www.learnentityframeworkcore.com/conventions

Foreign Key Shadow Properties:
If you choose not to explicitly include a foreign key property in the dependant end of the relationship, EF Core will create a shadow property using the pattern Id.

But I still do not know if this is the case in all types of relationships.

I hope that helps some one.

UPDATE

Ok now I found exactly what I wanted to know and understand, please take a look on these links which include the difference conventions of creating the Models (entities):

http://www.learnentityframeworkcore.com/conventions/one-to-many-relationship

http://www.learnentityframeworkcore.com/conventions/one-to-one-relationship

5
11/4/2017 12:38:55 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow