名称不匹配时的实体框架外键

时间:2019-05-26 03:30:43

标签: c# entity-framework ef-code-first

我有一个名为AccountValues的表,我想从我的Account表中引用该表。但是,我希望不使用AccountValueIdAccountValue,而是通过在MostRecent_前面加上如下前缀来增强描述性:

[ForeignKey("AccountValues")]
public long MostRecent_AccountValuesId { get; set; }
public AccountValues MostRecent_AccountValues { get; set; }

我的问题是我应该在哪里放置ForeignKey属性,以便最终获得实际的外键(例如FK_Something),并且MostRecent_AccountValues也可以自动工作?

注意:我的约定是不要使用复数表名。但是这里AccountValues只是复数,因为每一行中都有许多不同的值。

1 个答案:

答案 0 :(得分:3)

问题在您的ForeignKey属性的public long MostRecent_AccountValuesId名称中。 ForeignKey名称应与导航属性名称匹配,如下所示:

[ForeignKey("MostRecent_AccountValues")] // <-- Here it is
public long MostRecent_AccountValuesId { get; set; }
public AccountValues MostRecent_AccountValues { get; set; }