除了相应的`navigation property`之外,我还应该定义`foreign key Id`属性吗?

Moh*_*din 5 .net c# entity-framework entity-framework-core .net-core

我在Microsoft教程中找到了这些实体:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我真的需要定义StudentId属性吗?

在不同类型的关系中,案件会有何不同?例如one-oneone-many

更新:

在这个链接http://www.learnentityframeworkcore.com/relationships中我们可以看到,在One-Many和Many-Many中,他们没有定义Id外键属性,但是在One-One中,他们做到了.这是为什么?

Moh*_*din 5

我在此页面中找到了我的问题的部分答案:

http://www.learnentityframeworkcore.com/conventions

外键影子属性:
如果您选择不在关系的依赖端显式包含外键属性,EF Core 将使用模式 Id 创建一个影子属性。

但我仍然不知道是否在所有类型的关系中都是如此。

我希望这对某人有所帮助。

更新

好的,现在我找到了我想知道和理解的内容,请查看这些链接,其中包括创建模型(实体)的不同约定:

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

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