0

因为我的实体类中有很多相同的字段,比如id,org_id等,所以我写了基类继承,这样每个子类就不需要再写这些字段了。

我有一个基类:</p>

[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); }
    }
}

派生子类:</p>

[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); }
    }
}

出现如下错误信息:</p>

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

如果我去掉基类的持久标识:</p>

[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); }
    }
}

派生子类:</p>

[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); }
    }
}

运行时提示错误:Property organizationdoes not exist within type Precise.Model.Models.Company

4

1 回答 1

0

如果给属性添加Association属性organization,则类中的集合属性Organization无法检索相关记录,因为BaseOrganization类是非持久的。

如果您不需要Organization类中的集合属性,请删除 Association 属性。如果删除它,您可以在基类上保留 NonPersistent 属性。

于 2020-05-01T04:10:21.820 回答