2017-01-11 5 views
2

このエラーには多くのスレッドがあり、そのうちの半数を読んでいます。 (その関係は1:1ではないため適用できませんでした)いくつか試しましたが、まだエラーが発生しています。「アソシエーションの主な終点を特定できません」というエラーメッセージが表示される

私は2つのテーブルAccountAccountSettingsを持っています。彼らは1対1の関係を持っています。ただし、Accountがプライマリです。それは、AccountSettingsなしで存在することができますが、それ以外の方法では存在しません。私が指定する方法

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.

:これは私がもともと持っていたものである

...その後

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; } 
} 

しかし、それは私にエラーが発生しましたAccountを原則としますか?

ありがとうございます!

答えて

1

私はついにそれを理解しました。うまくいけば、これは他の誰かを助けるでしょう。

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

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

AccountSettingを外部キーとして使用する必要はありません。この関係は、AccountSettingを追加してAccountIdを設定するときに作成されます。 –

+0

私がAcccountからAccountSettingsにナビゲートできるようにしたいからです。これは、AccountSettingIdを外部キーとして持つAccountテーブルなしで可能になりますか? –

+0

しかし、LazyLoading、ProxyCreation、 'include/load'の使用などのために、EFをどのように設定するかによって、多くの方法があります。しかし、通常は次のようにデータにアクセスします:Account.AccountSetting。 EFは通常、AccountySettingをAccountエンティティの子オブジェクト/エンティティに設定します。これは、データがデータベースから取得され、外部キーを介して両方向のナビゲーションを提供する場合です。 –

関連する問題