0

Because there are many of the same fields in my entity class, such as id, org_id, etc., I wrote the base class inheritance so that each subclass doesn't need to write these fields anymore.

I have a base class:

[OptimisticLocking(false), NonPersistent]
public class BaseOrganization : XPBaseObject
{
    public BaseOrganization(Session session) : base(session) { }

    private Organization _organization;
    [Association, NoForeignKey, Persistent("org_id")]
    public Organization organization
    {
        get { return _organization; }
        set { SetPropertyValue(nameof(organization), ref _organization, value); }
    }
}

Derived subclass:

[Persistent("company")]
public class Company : BaseOrganization
{
    public Company(Session session) : base(session) { }

    private string _code;
    [Size(100), Nullable(false), Indexed(nameof(organization), Name = "IX_company", Unique = true)]
    public string code
    {
        get { return _code; }
        set { SetPropertyValue(nameof(code), ref _code, value); }
    }

    private string _name;
    [Size(100), Nullable(false)]
    public string name
    {
        get { return _name; }
        set { SetPropertyValue(nameof(name), ref _name, value); }
    }
}

The following error message appears:

The association attribute cannot be used for member `Precise.Model.BaseOrganization.organization` because class `Precise.Model.BaseOrganization` is not persistent.

If I remove the persistent identity of the base class:

[OptimisticLocking(false)]
public class BaseOrganization : XPBaseObject
{
    public BaseOrganization(Session session) : base(session) { }

    private Organization _organization;
    [Association, NoForeignKey, Persistent("org_id")]
    public Organization organization
    {
        get { return _organization; }
        set { SetPropertyValue(nameof(organization), ref _organization, value); }
    }
}

Derived subclass:

[Persistent("company")]
public class Company : BaseOrganization
{
    public Company(Session session) : base(session) { }

    private string _code;
    [Size(100), Nullable(false), Indexed(nameof(organization), Name = "IX_company", Unique = true)]
    public string code
    {
        get { return _code; }
        set { SetPropertyValue(nameof(code), ref _code, value); }
    }

    private string _name;
    [Size(100), Nullable(false)]
    public string name
    {
        get { return _name; }
        set { SetPropertyValue(nameof(name), ref _name, value); }
    }
}

Prompt error at runtime:Property organization does not exist within type Precise.Model.Models.Company.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76

1 Answers1

0

If you add the Association attribute to the organization property, the collection property in the Organization class cannot retrieve related records, because the BaseOrganization class is non-persistent.

If you do not need a collection property in the Organization class, remove the Association attribute. If you remove it, you can keep the NonPersistent attribute on the base class.

Uranus
  • 1,690
  • 1
  • 12
  • 22
  • I need to use the association feature, and I have also removed the base class nonpersistent feature, but still reported an error:Property 'organization' does not exist within type 'Precise.Model.Models.Company' – shaohuatsou May 01 '20 at 16:09
  • If you remove NonPersistent attribute from the BaseOrganization class, you have two tables in the database: BaseOrganization and Company. And the organization column is in the BaseOrganization table. That is why, you cannot include the organization column in the index created for the Company table. With the NonPersistent attribute, you have only the Company table, and the organization column is in the Company table. – Uranus May 01 '20 at 16:14
  • Thank you for your reply. I have understood what you said. Does that mean that my idea can't be realized? – shaohuatsou May 01 '20 at 16:22