因为我的实体类中有很多相同的字段,比如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 organization
does not exist within type Precise.Model.Models.Company
。