【问题标题】:Castle ActiveRecord JoinedKey is not setCastle ActiveRecord JoinedKey 未设置
【发布时间】:2010-06-24 13:42:45
【问题描述】:

我正在使用“类表继承 - 使用连接的子类”,如下所述: 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?

【问题讨论】:

    标签: inheritance castle-activerecord hierarchy


    【解决方案1】:

    我认为你需要使用:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多