Castle ActiveRecord:一对一
在处理 castle 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();
}
然而,实际结果是两个对象都使用不同的密钥保存。我错过了什么吗?
While playing around with one-to-one associations in castle activerecord I stumbled upon the following problem:
I'm trying to model a one-to-one relationship (user-userprofile in this case). I already learned that this may not be a best practice, but let's ignore that for a moment (I'm still trying to understand what's going on).
[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; }
}
I would expect the following code to save a user with profile, yielding the same Id in the database:
[Test]
public void save_profile_saves_user()
{
var profile = new TestUserProfile
{
User = new TestUser()
};
profile.Save();
}
The actual result however is that both objects are saved with a different key. Am I missing something??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己找到了答案。定义 OneToOne 的关系一侧的 PrimaryKeyType 应该具有 PrimaryKeyType.Foreign 的 PrimaryKey:
返回更彻底地阅读文档...
I've found the answer myself. The PrimaryKeyType of the side of the relation where OneToOne is defined should have a PrimaryKey of PrimaryKeyType.Foreign:
Back to reading the docs more thoroughly...