【问题标题】:Empty property in Binding the GridViewColumn to List<EntityFrameworkCore's Entity>将 GridViewColumn 绑定到 List<EntityFrameworkCore's Entity> 中的空属性
【发布时间】:2021-12-05 18:36:16
【问题描述】:
  • 当我绑定另一个实体的名称时,我的 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 的功能导致数据库的脚手架错误吗?

【问题讨论】:

  • @AndriyShevchenko,似乎我将不得不创建类似于局部变量“var list = DBContext.AnotherEntities.ToList();”的东西在 _entities = DBContext.Entities.ToList();之前,因为只在查询中包含 AnotherEntities 和 Entities 是行不通的。

标签: c# wpf entity-framework xaml entity-framework-core


【解决方案1】:

我无法访问您的完整代码库,但我认为这是由于 EF Core 延迟加载功能及其动态代理可能不适用于 WPF 绑定。 在你的代码中改变这个

_entities = DBContext.Entities.ToList();

到这里

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

【讨论】:

    猜你喜欢
    • 2013-12-19
    • 2017-09-14
    • 2011-07-06
    • 1970-01-01
    • 2015-03-14
    • 2016-05-09
    • 2017-04-08
    • 2015-01-02
    • 2015-05-07
    相关资源
    最近更新 更多