2009-11-10 88 views
1

雖然有一個對一個協會玩弄在城堡的ActiveRecord我偶然發現了以下問題:城堡的ActiveRecord:一到一個

我試圖模擬一個一比一的關係(用戶在這種情況下是用戶配置文件)。我已經瞭解到這可能不是最佳做法,但我們暫時忽略這一點(我仍然試圖瞭解發生了什麼)。

[ActiveRecord] 
public class TestUser : ActiveRecordBase<TestUser> 
{ 
    [PrimaryKey(PrimaryKeyType.GuidComb)] 
    public Guid Id { get; set; } 


} 

[ActiveRecord] 
public class TestUserProfile : ActiveRecordBase<TestUserProfile> 
{ 
    [PrimaryKey(PrimaryKeyType.GuidComb)] 
    public Guid Id { get; set; } 

    [OneToOne(Cascade = CascadeEnum.All, Fetch = FetchEnum.Join)] 
    public TestUser User { get; set; } 
} 

我希望下面的代碼保存用戶使用配置文件,產生相同的ID在數據庫:

[Test] 
    public void save_profile_saves_user() 
    { 
     var profile = new TestUserProfile 
     { 
      User = new TestUser() 
     }; 

     profile.Save(); 

    } 

實際結果卻是,無論對象是保存使用不同的密鑰。我錯過了什麼?

回答

3

我自己找到了答案。其中OneToOne定義關係的側面的PrimaryKeyType應該有PrimaryKeyType.Foreign的的PrimaryKey:

[ActiveRecord] 
public class TestUserProfile : ActiveRecordBase<TestUserProfile> 
{ 
    [PrimaryKey(PrimaryKeyType.Foreign)] 
    public Guid Id { get; set; } 

    [OneToOne(Cascade = CascadeEnum.All, Fetch = FetchEnum.Join)] 
    public TestUser User { get; set; } 

} 

回到更徹底地閱讀文檔...

相關問題