gpt4 book ai didi

c# - Entity Framework 6 : Master-Detail view shows single HashSet instead of filtered detail items

转载 作者:行者123 更新时间:2023-11-28 23:20:13 27 4
gpt4 key购买 nike

我正在尝试基于由 MySQL 数据库支持的 Entity Framework 6 DbContext 实现基本的主从 View 。表格配置尽可能简单:

ER Diagram showing one-to-many relationship between two simple tables

我的测试应用程序的表单包含两个 ComboBox 元素 - 一个用于 master 类别,一个用于子类别(在我的例子中,master 是电子元件的类型,例如“IC”和细节更具体,如“FPGA”、“微 Controller ”等)。

Two Comboboxes ("MainType" and "SubType"), with the lower one showing a class name instead of a list of items

每个 ComboBoxDataSource 属性都通过表单设计器设置为它们自己的 BindingSource

绑定(bind)源本身以不同的方式设置了它们的 DataSource 属性,遵循我发现的各种教程:

  • Master BindingSource:通过 BindingList
  • 设置为表示 comp_main_type 表的 DbSet
  • Detail BindingSource:设置为主绑定(bind)源,DataMember 属性设置为从 master 到 detail 的“导航属性”
ExampleEntities entities = new ExampleEntities();
entities.comp_main_type.Load();
entities.comp_sub_type.Load();

BindingSourceCompMainType.DataSource = entities.comp_main_type.Local.ToBindingList();

BindingSourceCompSubType.DataSource = BindingSourceCompMainType;
BindingSourceCompSubType.DataMember = "comp_sub_type";

部分地,这按预期工作 - 主 ComboBox 被正确填充,但细节 ComboBox 不是。 HashSet 的类型名称显示为唯一的项目,而不是显示单个项目:

System.Collections.Generic.HashSet`1[ExampleApp.comp_sub_type]

调试器显示此 HashSet 确实包含由所选主项过滤的正确项:

Debugger showing the items of the ComboBox items list

我无法找到造成这种情况的原因 - 为什么详细信息 ComboBox 不是用 HashSet 的项目填充,而是用 HashSet 本身?

最佳答案

检查 Reza Aghaei 提供的链接后,问题的原因变得非常明显:不知何故,我跳过了指南的一个重要部分。

需要添加如下类:

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Data.Entity;

namespace WinFormswithEFSample
{
public class ObservableListSource<T> : ObservableCollection<T>, IListSource
where T : class
{
private IBindingList _bindingList;

bool IListSource.ContainsListCollection { get { return false; } }

IList IListSource.GetList()
{
return _bindingList ?? (_bindingList = this.ToBindingList());
}
}
}

之后,必须修改代码生成模板:

Find the ProductModel.tt file which will be nested under the ProductModel.edmx file

Double-click on the ProductModel.tt file to open it in the Visual Studio editor

Find and replace the two occurrences of “ICollection” with “ObservableListSource”. These are located at approximately lines 296 and 484.

Find and replace the first occurrence of “HashSet” with “ObservableListSource”. This occurrence is located at approximately line 50. Do not replace the second occurrence of HashSet found later in the code.

Save the ProductModel.tt file. This should cause the code for entities to be regenerated. If the code does not regenerate automatically, then right click on ProductModel.tt and choose “Run Custom Tool”.

来源:https://msdn.microsoft.com/en-us/library/jj682076(v=vs.113).aspx

应用这些更改后,详细 View 按预期工作。

关于c# - Entity Framework 6 : Master-Detail view shows single HashSet instead of filtered detail items,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com