[英]How do I establish a one to one relationship with Entity Framework Code First
我一直在閱讀有關在EF
中建立一對一關系的人的所有Google和SO頁面,但這對我不起作用。
這是我的模型:
帳戶:
public int Id {get; set;}
public virtual AccountConfig AccountConfig {get; set;}
帳戶地圖:
HasKey(t => t.Id);
HasRequired(t => t.AccountConfig)
.WithOptional();
帳戶配置:
public int Id {get; set;}
public int AccountId {get; set;}
public virtual Account Account {get; set;}
帳戶配置圖:
HasKey(t => t.Id);
HasRequired(t => t.Account)
.WithOptional(t => t.AccountConfig);
在執行時, AccountConfig
財產Account
是NULL
和Account
上的財產AccountConfig
是不正確的記錄(巧合的是,檢索Account.Id
是一樣的AccountConfig.Id
,但我不知道這意味着什么)。
在數據庫中, Account
表沒有對AccountConfig
記錄的引用,但AccountConfig
表使用AccountId
列引用了Account
記錄。
最終結果是我可以從Account
引用AccountConfig
,並且(如果可能的話)引用AccountConfig
Account
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.