我正在尝试确定我当前使用 Table-Per-Type 层次结构的设计是否是我进行以下操作的最佳方式:
例如,我有 3 个子类(Manager、Executive、Employee),它们都继承自我的基本 Person。然后,我想在网格视图中显示 Person 的所有实例,以供最终用户选择并编辑其关联数据。但是,我的问题与选择 Person 时如何查询适当的类型有关。我目前正在为 Person 类做如下操作:
public class Person
{
Guid PersonID { get; set; }
string FirstName { get; set; }
string LastName { get; set; }
string PersonType { get; set; }
}
PersonType
然后,当它们被实例化时, 我在每个子类中设置该字段。
设置绑定并连接到SelectionChanged
事件的委托
BindingSource peopleBinding = new BindingSource();
peopleBinding.DataSource = db.People.Local.ToBindingList();
this.peopleGridView.DataSource = peopleBinding;
this.peopleGridView.SelectionChanged += new EventHandler(peopleGridView_SelectionChanged);
GridView 的 SelectionChanged 事件
if (peopleGridView.SelectedRows.Count != 1)
{
return;
}
Person person = peopleBinding.Current as Person;
if (person == null)
{
return;
}
switch (person.PersonType)
{
case "Employee":
Employee employee = db.Employees.Find(person.PersonID);
// Do Work With Employee
break;
case "Manager":
Manager manager = db.Managers.Find(person.PersonID);
// Do Work With Manager
break;
case "Executive":
Executive executive = db.Executives.Find(person.PersonID);
// Do Work With Executive
break;
default:
throw new ArgumentException (string.Format("Invalid type of person encountered ({0})", person.PersonType);
}
有没有更好的方法来获取子类的实例,而不是使用该PersonType
字段作为某种鉴别器来确定DbSet
我需要查询什么来获取关联实体?