2010-06-24 66 views
1

我使用“类表继承 - 使用加入了子类”如下所述: http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html城堡的ActiveRecord JoinedKey未设置

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

[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属性包含正确的值。

编辑:

我也许应该补充一点,我们的实体会自动生成,我不想碰发电机。

EDIT2:

好吧,我知道我必须触摸发电机,以使其发挥作用。但仍然为什么不是Active Record设置Comp_id?

问:

我怎么能告诉ActiveRecord的也设置JoinedKey的价值在子类中,这样CompId ==标识?

回答

0

我认为你需要使用:

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

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

0

这是一个相当古老的问题,但我遇到了同样的问题。 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 
    } 
    .... 
} 

我刚刚成功地与我喜欢的类型层次结构进行了测试。

相关问题