【发布时间】:2016-03-28 12:42:16
【问题描述】:
我在 Visual Studio 2015 中使用 Entity Framework 6 来创建一个带有 DataGrid 的 WPF 应用程序。它需要绑定到包含 3 级深度的实体框架导航属性的搜索结果:一个员工可以有任意数量的EmployeeStatus,一个EmployeeStatus 可以有一个EmployeeStatusDescription 实体。所以它看起来像这样:
员工 > 员工状态 > 员工状态描述
我有以下Include()
var comparison = Expression.Lambda<Func<Employee, bool>>(
Expression.Equal(
Expression.Property(paramEmployee, selectedColumnValue),
Expression.Constant(SearchValue)),
paramEmployee).Compile();
var query = (from e in Context.Employees
.Include("EmployeeStatus.EmployeeStatusDescription")
.Where(comparison)
select e);
我尝试像这样绑定 EmployeeStatusDescription:
<DataGrid ItemsSource="{Binding SearchResults}">
<!-- other columns here... -->
<DataGridTextColumn Binding="{Binding
EmployeeStatus.EmployeeStatusDescription.description}" Header="Status" />
</DataGrid>
但是对于具有EmployeeStatus 和关联导航属性EmployeeStatusDescription 的Employee,Status 列显示为空白。
但是,在数据库上,下面的 SQL 带回 EmployeeStatusDescription 就好了:
select esd.* from employee e
left join EmployeeStatus es on e.employeeID = es.employeeID
left join EmployeeStatusDescription esd on
es.employeeStatusDescriptionID = esd.employeeStatusDescriptionID
where e.employeeID = '30299'
我做错了什么?感谢您的帮助。
更新 1:EmployeeStatus 在我的 Employee 模型中是这样定义的,所以它是一个集合,而不仅仅是一个值:
public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }
更新 2:这是 EmployeeStatusDescription 类:
public partial class EmployeeStatusDescription
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public EmployeeStatusDescription()
{
this.EmployeeStatus = new HashSet<EmployeeStatu>();
}
public int employeeStatusDescriptionID { get; set; }
public string employeeStatusAbbreviation { get; set; }
public string description { get; set; }
public bool isActive { get; set; }
public System.DateTime createdDate { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }
}
【问题讨论】:
-
EmployeeStatus 对象上的 EmployeeStatusDescription 属性是刚刚称为 EmployeeStatusDescription 还是其他名称?
标签: c# wpf entity-framework data-binding datagrid