关于此错误的讨论有很多,我已经阅读了其中的 ~6 个。 (其中一些不适用,因为关系不是 1:1。)我已经尝试了一些方法,但我仍然遇到错误。
我有两个表,Account
和 AccountSettings
。他们是 1:1 的关系。但是,Account
是主要的。它可以在没有 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
作为委托(delegate)人?
谢谢!
最佳答案
我终于明白了。希望这会帮助别人。在非主表中:
public class AccountSetting
{
[Key]
public int AccountSettingID { get; set; }
[Required]
public int AccountID { get; set; }
[Required, ForeignKey("AccountID ")]
public virtual Account Account { get; set; }
}
关于c# - 不确定如何解决 'Unable to determine the principal end of an association' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41601128/