Castle ActiveRecord:一对一

发布于 2024-08-10 23:06:57 字数 876 浏览 7 评论 0原文

在处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-08-17 23:06:57

我自己找到了答案。定义 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; }

 }

返回更彻底地阅读文档...

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:

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

 }

Back to reading the docs more thoroughly...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文