locked
How to get a bindable collection from EF4? RRS feed

  • Question

  • I'm experimenting with databinding using WPF and EF4.

    I generated my model first, which includes a Product entity.

    I'd like to have a databound-property that holds a collection of product entities, and supports change notification (when an item is added/deleted)

    Currently I'm converting the resulting ObjectSet<Product> into an ObservableCollection, but there must be an easier way. Now, I have to synchronize two collections.

    Any help on this is appreciated!
    Sunday, November 22, 2009 8:56 PM

Answers

  • Hello,

    I am not convinced I understand your question, but I will give it a try. Please clarify if I am missing what you meant.

    Entity Framework normally produces its own binding lists from queries, query results and collections. Those binding lists implement IBindingList but not any of the WPF specific interfaces and notifications. Typically these binding lists can work well with WPF, although there are some caveats, such as the fact that they currently don't support sorting.

    There are some details worth considering on how to obtain this binding list. For instance, the recommendation is to bind to query results rather than the query itself, to avoid multiple executions of the query:

    viewSource.Source = query.Execute(MergeOption.AppendOnly);

    Here is another alternative that is useful if you need access to the binding list:

    IBindingList bindingList = ((IListSource)query).GetList();
    viewSource.Source = bindingList;

    Yet, there are reasons you might want to create an ObservableCollection as you mention, if for instance you need sorting. In the following blog post you can find a simple implementation of this:

    http://blogs.msdn.com/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, November 23, 2009 4:54 AM
    Moderator

All replies

  • Hello,

    I am not convinced I understand your question, but I will give it a try. Please clarify if I am missing what you meant.

    Entity Framework normally produces its own binding lists from queries, query results and collections. Those binding lists implement IBindingList but not any of the WPF specific interfaces and notifications. Typically these binding lists can work well with WPF, although there are some caveats, such as the fact that they currently don't support sorting.

    There are some details worth considering on how to obtain this binding list. For instance, the recommendation is to bind to query results rather than the query itself, to avoid multiple executions of the query:

    viewSource.Source = query.Execute(MergeOption.AppendOnly);

    Here is another alternative that is useful if you need access to the binding list:

    IBindingList bindingList = ((IListSource)query).GetList();
    viewSource.Source = bindingList;

    Yet, there are reasons you might want to create an ObservableCollection as you mention, if for instance you need sorting. In the following blog post you can find a simple implementation of this:

    http://blogs.msdn.com/bethmassi/archive/2009/05/08/using-the-wpf-observablecollection-with-ef-entities.aspx

    Hope this helps,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, November 23, 2009 4:54 AM
    Moderator
  • Hi Diego,

    Some guidance doing this kind of thing using the MVVM pattern would be great.

    While Beth's articles are always informative, they also always seem to be a bit "quick & dirty", showing how things work for sure, but not really adhering to any production "best practices".

    Thanks,

    Yann

    Tuesday, February 15, 2011 10:52 PM