是一个ObservableCollection实现IListSource是否适用于EF 6导航属性?

时间:2017-03-12 21:15:48

标签: c# winforms entity-framework datagridview

我刚刚开始使用Entity Framework,我在一个面向.Net 4.5.2的Windows窗体应用程序中使用EF 6.1.3(没有特别的原因,为什么它不能成为后来的.Net版本)。

我想将我的数据绑定到Windows窗体DataGridView,我在MSDN Entity Framework Databinding with WinForms上找到了这个演练。该页面最后更新于2016年10月,但似乎更旧(这意味着Visual Studio 2013是最新版本)。

该页面建议创建一个ObservableListSource类,该类继承自ObservableCollection并实现IListSourceIListSource.Getlist实现只返回通过调用{{IList创建的ToBindingList 1 {} ObservableCollection),就像这样:

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());
    }
}

然后建议更改POCO(实体)类中导航属性的类型,这些类型指向从IListICollectionObservableListSource的子实体,如下所示:

public class Category
{
    private readonly ObservableListSource<Product> _products =
            new ObservableListSource<Product>();

    public int CategoryId { get; set; }
    public string Name { get; set; }
    public virtual ObservableListSource<Product> Products { get { return _products; } }
}

当我的导航属性被声明为DataGridView时,我的IList 出现了,但是我可能尚未对所有方面进行测试。我的问题是 EF 6 / .Net 4.5中是否有任何好处,其导航属性的类型为ObservableCollection实现IListSource ,或者是一种解决方法在早期版本中需要吗?

我意识到我可以继续创建ObservableListSource类并将其用于我的所有导航属性,但我不想让我的代码复杂化,而不是必需的(并可能导致问题)。

0 个答案:

没有答案