不确定如何解决 'Unable to determine the principal end of an association' 错误

Not sure how to get around 'Unable to determine the principal end of an association' error

关于此错误的讨论有很多,我已经阅读了其中的 ~6 个。 (其中一些不适用,因为关系不是 1:1。)我已经尝试了一些方法,但我仍然遇到错误。

我有两个表,AccountAccountSettings。他们有 1:1 关系。但是,Account 是主要的。它可以存在 w/o AccountSettings,但反之则不行。

这是我原来的...

public class Account
{
    [Key]
    public int AccountID { get; set; }
    public int? AccountSettingID { get; set; }

    [ForeignKey("AccountSettingID ")]
    public virtual AccountSetting AccountSetting { get; set; }
}

然后...

public class AccountSetting 
{
    [Key]
    public int AccountSettingID { get; set; }
    public int AccountID { get; set; }

    [ForeignKey("AccountID ")]
    public virtual Account Account { get; set; }
}

但这让我犯了错误:

Unable to determine the principal end of an association between the types 'EZHomes.Data.Model.AccountSetting' and 'EZHomes.Data.Model.Account'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

如何指定Account为原则?

谢谢!

我终于明白了。希望这会帮助别人。在 non-primary table:

public class AccountSetting 
{
    [Key]
    public int AccountSettingID { get; set; }
    [Required]
    public int AccountID { get; set; }

    [Required, ForeignKey("AccountID ")]
    public virtual Account Account { get; set; }
}