【发布时间】:2017-06-07 20:05:27
【问题描述】:
我有两类 Student 和 StudentAddress。学生有一个地址。现在如何使用 Fluent API 将 Student 类的主键 StudentId 设置为 StudentAddress 类的外键。我想要一个不同的属性,例如 StudentId 将是 StudentAddress 中的外键。我怎样才能做到这一点? (我正在使用实体框架 6)。这是我的课。
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
//Navigation property
public virtual StudentAddress StudentAddress { get; set; }
}
public class StudentAddress
{
public int StudentAddressId { get; set; }
public int StudentId { get; set; } //Set this property as a forign key
public string Address { get; set; }
//Navigation property
public virtual Student Student { get; set; }
}
【问题讨论】:
-
你不能那样做。一般的技术是让StudentAddress 的StudentId 成为PK 和FK。见here
-
是的,我也看到了。但我不确定是我的编码问题还是我遗漏了什么。谢谢。
标签: asp.net-mvc entity-framework-6 ef-fluent-api