开发者

Castle ActiveRecord JoinedKey is not set

开发者 https://www.devze.com 2023-01-04 19:39 出处:网络
I am using \"Class Table Inheritance - Using joined subclasses\" as described here: http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html

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 com开发者_如何转开发p_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?


I think you need to use:

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

... and the example they give is wrong.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号