如何绑定Entity Framework关联的对象

时间:2012-03-15 15:10:33

标签: c# asp.net .net entity-framework entity-framework-4

这个解决方案是我试图实现的。

在EF模型中,我有两个关联实体(2个连接表)

  

InstitutionEmployeeProcessingDepartment(InstitutionEmployeeProcessingDepartementId, ProcessingDepartmentId ,...)
  ProcessingDepartment(的 ProcessingDepartmentId 下,...)

我将它绑定到某个列表框。是否有可能在EF

中做这样的事情
List<InstitutionEmployeeProcessingDepartment> institutionEmployeeProcessingDepartments = institutionEmployee.InstitutionEmployeeProcessingDepartments.ToList();
lbProcessingDepartment.DataSource = institutionEmployeeProcessingDepartments;  
lbProcessingDepartment.DataValueField = "InstitutionEmployeeProcessingDepartementId";
lbProcessingDepartment.DataTextField = "ProcessingDepartment.Name";

这是关键点lbProcessingDepartment.DataTextField =“ ProcessingDepartment.Name ”;

我得到的错误是DataBinding:'DataModel.InstitutionEmployeeProcessingDepartment'不包含名为'ProcessingDepartment.Name'的属性。

我将此表包含在.Include()方法中。

1 个答案:

答案 0 :(得分:0)

这是我设法实现的一个解决方案,但我完全不喜欢它。 因为在我对我的问题的第二个评论中,我注意到我能够显示对象名称空间。所以我将这段代码添加到ProcessingDepartment类中的EF cs文件中。它只是覆盖了ToString()方法

    public override String ToString()
    {
        return this.Name;
    }

使用此选项lbProcessingDepartment.DataTextField = "ProcessingDepartment";

这有效,但对我来说看起来像是黑客。