我有一个有3列的实体类; entityType,entityID和EntityText。我想从3个条件表中填充entityText列。例如:如果实体类型为3(联系人)且entityID为2,则它将连接到Contacts表,并将通过entityID获取联系人,并将contact.name属性设置为EntityText。我怎么能这样做?
详细信息: 不是同一个对象类。
Contact
{
contactID
firstName
lastName
..
}
Company
{
companyID
name
address
...
}
Task{
taskID
entityID
entityType
entityText= Company.name or Contact.firstName + Contact.lastName
...
}
答案 0 :(得分:0)
编辑:重构以匹配更新的问题
因为EntityText的内容背后有逻辑,我会优先考虑多态性
public abstract class Task
{
public virtual int Id { get; set; }
public abstract string Text { get; set; }
}
public class ContactTask : Task
{
public virtual Contact Contact { get; set; }
public override string Text
{
get { return Contact.FirstName + " " + Contact.LastName; }
set { Contact.FirstName = value.Split(' ')[0]; Contact.LastName = value.Split(' ')[1]; }
}
}
public class CompanyTask : Task
{
public virtual Company Company { get; set; }
public override string Text
{
get { return Company.Name; }
set { Company.Name = value; }
}
}
public class TaskMap : ClassMap<Task>
{
public TaskMap()
{
Table("TaskTable");
Id(c => c.Id, "taskID").GeneratedBy....();
DiscriminateSubclassesOn("entityType");
}
}
public class ContactTaskMap : ClassMap<ContactTask>
{
public ContactTaskMap()
{
DiscriminatorValue(3);
References(ct => ct.Contact, "entityID");
}
}
public class CompanyTaskMap : ClassMap<CompanyTask>
{
public ContactTaskMap()
{
DiscriminatorValue(2);
References(ct => ct.Company, "entityID");
}
}
// get all tasks
var all = session.QueryOver<Task>().List();
// get all tasks for Company
var companyTasks = session.QueryOver<CompanyTask>().List();