下午好,
我正在尝试实现实体框架,但在尝试利用主从关系时似乎遇到了问题。理想情况下,我希望能够从组合框中选择一家公司,然后用所选公司的员工姓名列表填充后续组合框。我似乎遇到了一些问题,试图通过多对多关系实现这一点,该关系有一个包含一些附加数据的桥接表。以下是我正在使用的三个数据库表的简化版本:
create table Organization.Company
(
CompanyID int not null,
CompanyName varchar(40) not null,
ServiceRepresentative varchar(20) not null,
primary key (CompanyID)
)
create table Person.Employment
(
CompanyID int not null,
PersonID uniqueidentifier not null,
EmployeeID int not null,
FullTime bit not null,
HoursPerWeek float null,
primary key (CompanyID, PersonID),
constraint fk_Company_Employment foreign key (CompanyID) references Organization.Company(CompanyID),
constraint fk_Person_Employment foreign key (PersonID) references Person.ContactInfo(PersonID)
)
create table Person.ContactInfo
(
PersonID uniqueidentifier not null,
FirstName varchar(40) not null,
LastName varchar(40) not null,
primary key (PersonID)
)
我的代码隐藏我有以下(WinForms):
private orgEntities db;
private BindingSource companyBinding;
private BindingSource employeeBinding;
public MainForm()
{
InitializeComponent();
this.db = new orgEntities();
this.companyBinding = new BindingSource();
this.employeeBinding = new BindingSource();
this.companyBinding.DataSource = db.Companies.OrderBy(x => x.CompanyName);
this.employeeBinding.DataSource = companyBinding;
this.employeeBinding.DataMember = "Employment";
this.currentCompanyComboBox.DataSource = companyBinding;
this.currentCompanyComboBox.DisplayMember = "CompanyName";
this.currentCompanyComboBox.ValueMember = "CompanyID";
this.currentEmployeeComboBox.DataSource = employeeBinding;
this.currentEmployeeComboBox.DisplayMember = "ContactInfo.FullName"; // Generated by extension class
this.currentEmployeeComboBox.ValueMember = "PersonID";
}
The issue I seem to be running into is that only one employee is displayed inside the combobox when a company is selected. 当我将“ContactInfo.FullName”更改为“EmployeeID”时,我得到了 EmployeeID 的完整列表。我假设实体框架正在抓取第一个人并导航到他们的 FullName 属性,这导致他们的信息是唯一显示的内容。
有没有什么办法可以让我在这个下拉列表中显示完整的姓名列表,同时存在桥接表(Person.Employment),或者我最好创建一个将就业和联系信息表组合在一起的视图并使用它在 edmx 文件而不是两个单独的表中?
我知道我可能会以错误的方式解决这个问题,如果有人能指出我在这方面的正确方向,我将不胜感激。谢谢你,祝你有美好的一天!