将 GridViewColumn 绑定到 List<EntityFrameworkCore 的实体中的空属性>


  • 当我绑定另一个实体的名称时,我的WPF (netcore3.1-windows) EFCore 5应用程序中没有初始数据。
  • 数据最初在同一个应用程序中加载,但使用WPF (net40) EFCore 6应用程序从页面加载:
public partial class Entity
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]

public virtual AnotherEntity AnotherEntity { get; set; }
...
}
...
WPFPage()
{
_entities = DBContext.Entities.ToList();
}
<ListView ItemsSource="{Binding _entities}" ...> ...
<GridView>                    
<!--with FECore5 no data here. But in EF6 there is.-->
<GridViewColumn DisplayMemberBinding="{Binding Entity.AnotherEntityName}" 
Header="Entity"
Width="100"></GridViewColumn>
</GridView>
...

除非我访问和加载另一个数据库实体从AnotherEntities属性在应用程序代码:

public AnotherWPFPage() { _anotherEntities = DBContext.AnotherEntities.ToList(); }

或者我显式地将绑定设置为

<GridViewColumn DisplayMemberBinding="{Binding Entity.AnotherEntity.AnotherEntityName}" 

这是由于数据库的脚手架错误由Fluent Api, xaml绑定功能或EntityFrameworkCore的功能?

我没有访问您的完整代码库,但我认为这是由于EF Core延迟加载特性及其动态代理可能无法与WPF绑定一起工作。在你的代码中修改

_entities = DBContext.Entities.ToList();

_entities = DBContext.Entities
.Include(x => x.AnotherEntity)
.ToList();

最新更新