【发布时间】:2014-04-02 22:20:42
【问题描述】:
我得到了一个数据库,我需要为它设置 Code First。我想我已经完成了,我现在遇到的问题是我想更改实体中的一些外键名称以使其更有意义。
假设我有两张表,Person 和 Location,看起来像这样:
Person
------
PersonId int not null (PK)
DefaultLocation int not null (FK)
Location
--------
LocationId int not null (PK)
对于位置实体,我想我知道了:
public class Location
{
[Key]
public int LocationId { get; set; }
[InverseProperty("DefaultLocation")]
public List<Person> PeopleWithDefault { get; set; }
}
现在,我想要在我的 Person 实体上拥有 2 个属性,一个 DefaultLocation 导航属性和一个用于外键的 DefaultLocationId 属性。这就是我现在所处的位置:
public class Person
{
[Key]
public int PersonId { get; set; }
[ForeignKey("Location")]
[Column("DefaultLocation")]
[Required]
public int DefaultLocationId { get; set; }
public virtual Location DefaultLocation { get; set; }
}
这是哪个坏孩子扔的:
类型“Person”的属性“DefaultLocationId”上的 ForeignKeyAttribute 无效。在依赖类型“人员”上找不到导航属性“位置”。 Name 值应该是有效的导航属性名称。
所以,这个错误非常有意义......我只是不知道如何修复它。我显然在某处遗漏了注释,或者错误地使用了 [ForeignKey]。谁能帮帮我?
【问题讨论】:
标签: c# entity-framework ef-code-first code-first