城堡的ActiveRecord:一到一个
-
19-09-2019 - |
题
尽管在城堡的activerecord我偶然发现存在以下问题玩弄一个一对一关联:
我试图建模一对一的关系(用户USERPROFILE在这种情况下)。我已经了解到,这可能不是最好的做法,但让我们忽略了一会儿(我还是想知道,这是怎么回事)。
[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();
}
实际的结果却是,这两个对象将被保存用不同的密钥。我缺少的东西??
解决方案
我发现自己的答案。其中OneToOne定义应具有PrimaryKeyType.Foreign的PrimaryKey的关系的侧的PrimaryKeyType:
[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; }
}
回到更彻底地读取文档...
不隶属于 StackOverflow