将儿童组合框与多对多关系进行数据绑定

时间:2012-10-12 20:31:30

标签: c# winforms entity-framework c#-4.0 data-binding

下午好,

我正在尝试实现实体框架,但在我尝试使用主 - 细节关系时似乎遇到了问题。理想情况下,我希望能够从组合框中选择一个公司,然后使用所选公司的员工名称列表填充后续组合框。我似乎遇到了试图通过多对多关系实现这一目标的问题,该关系具有包含一些附加数据的桥接表。以下是我正在使用的三个数据库表的简化版本:

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";           
    }

我似乎遇到的问题是,当选择公司时,组合框内只显示一名员工。当我将“ContactInfo.FullName”更改为“EmployeeID”时,我会获得EmployeeID的完整列表。我假设实体框架抓住第一个人并导航到他们的FullName属性,这导致他们的信息是唯一显示的东西。

有没有什么办法可以在这个下拉列表中显示完整的名单列表,其中包含桥表(Person.Employment),或者我最好只创建一个将Employment和ContactInfo表组合在一起的视图并在edmx文件中使用它而不是两个单独的表?

我明白我可能会以错误的方式解决这个问题,如果有人能指出我在这个问题上的正确方向,我将不胜感激。谢谢你,祝你有个美好的一天!

0 个答案:

没有答案