Castle ActiveRecord JoinedKey 未设置

发布于 2024-09-07 01:19:03 字数 1083 浏览 8 评论 0原文

我正在使用“类表继承 - 使用连接的子类”,如下所述: http://www.castleproject.org/activerecord/documentation/trunk/ usersguide/typehierarchy.html

下面的代码部分是从那里复制的。

[ActiveRecord("entity"), JoinedBase]
public class Entity : ActiveRecordBase
{
    ...
    private int id;

    [PrimaryKey]
    private int Id
    {
        get { return id; }
        set { id = value; }
    }
}

[ActiveRecord("entitycompany")]
public class CompanyEntity : Entity
{
    private int comp_id;

    [JoinedKey("comp_id")]
    public int CompId
    {
        get { return comp_id; }
        set { comp_id = value; }
    }
    ....
}

现在,当我加载 CompanyEntity 并访问 ComId 属性时,它始终为 0,但继承的 Id 属性包含正确的值。

编辑:

我可能应该补充一点,我们的实体是自动生成的,我不想触摸生成器。

编辑2:

好吧,我意识到我必须触摸发电机才能使其工作。但为什么 Active Record 不设置 Comp_id 呢?

问题

如何告诉 ActiveRecord 在子类中也设置 JoinedKey 的值,以便 CompId == Id?

I am using "Class Table Inheritance - Using joined subclasses" as described here:
http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html

The following code is partly copied from there.

[ActiveRecord("entity"), JoinedBase]
public class Entity : ActiveRecordBase
{
    ...
    private int id;

    [PrimaryKey]
    private int Id
    {
        get { return id; }
        set { id = value; }
    }
}

[ActiveRecord("entitycompany")]
public class CompanyEntity : Entity
{
    private int comp_id;

    [JoinedKey("comp_id")]
    public int CompId
    {
        get { return comp_id; }
        set { comp_id = value; }
    }
    ....
}

Now when I have a CompanyEntity loaded and access the ComId property it is always 0, but the inherited Id Property contains the correct value.

Edit:

I Probably should add that our Entities are automatically generate and I do not want to touch the generator.

Edit2:

Ok, I realize that I have to touch the generator in order to make it work. But still why isn't Active Record setting the Comp_id?

Question:

How can I tell ActiveRecord to also set the value of the JoinedKey in the child class so that CompId == Id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

谁与争疯 2024-09-14 01:19:03

我认为你需要使用:

[JoinedKey("comp_id")]
public override int Id { get { return base.Id; } }

...他们给出的例子是错误的。

I think you need to use:

[JoinedKey("comp_id")]
public override int Id { get { return base.Id; } }

... and the example they give is wrong.

歌入人心 2024-09-14 01:19:03

这是一个很老的问题,但我遇到了同样的问题。 Castle.ActiveRecord 页面上的示例是错误的。

您可以像这样解决问题(带有注释修改的代码示例):

[ActiveRecord("entity"), JoinedBase]
public class Entity : ActiveRecordBase
{
    ...
    protected int id; // use protected instead of private

    [PrimaryKey]
    private int Id
    {
        get { return id; }
        set { id = value; }
    }
}

[ActiveRecord("entitycompany")]
public class CompanyEntity : Entity
{
    // private int comp_id; // this member variable is not required

    [JoinedKey("comp_id")]
    public int CompId
    {
        get { return id; } // access the member variable of the base class
        set { id = value; } // access the member variable of the base class
    }
    ....
}

我刚刚用我的类型层次结构成功地测试了它。

This is quite an old question, but I ran into the same problem. The example on the Castle.ActiveRecord page is wrong.

You can solve the problem like this (your code example with commented modifications):

[ActiveRecord("entity"), JoinedBase]
public class Entity : ActiveRecordBase
{
    ...
    protected int id; // use protected instead of private

    [PrimaryKey]
    private int Id
    {
        get { return id; }
        set { id = value; }
    }
}

[ActiveRecord("entitycompany")]
public class CompanyEntity : Entity
{
    // private int comp_id; // this member variable is not required

    [JoinedKey("comp_id")]
    public int CompId
    {
        get { return id; } // access the member variable of the base class
        set { id = value; } // access the member variable of the base class
    }
    ....
}

I've just successfully tested it with my type hierarchy.

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