0

I am getting the following Exception

NotSupportedException: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery)

Stack Trace:

   at System.Data.Entity.Infrastructure.DbQuery`1.System.ComponentModel.IListSource.GetList()
   at MS.Internal.Data.ViewManager.GetViewRecord(Object collection, CollectionViewSource cvs, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at MS.Internal.Data.DataBindEngine.GetViewRecord(Object collection, CollectionViewSource key, Type collectionViewType, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, Boolean createView, Func`2 GetSourceItem)
   at System.Windows.Data.CollectionViewSource.GetDefaultCollectionView(Object source, DependencyObject d, Func`2 GetSourceItem)
   at System.Windows.Controls.ItemCollection.SetItemsSource(IEnumerable value, Func`2 GetSourceItem)
   at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
   at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
   at System.Windows.Data.BindingExpression.Activate(Object item)
   at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
   at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
   at MS.Internal.Data.DataBindEngine.Run(Object arg)
   at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
   at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.UIElement.UpdateLayout()

MainWindow.xaml

<Window x:Class="TryingWPFWithUltimate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TryingWPFWithUltimate;assembly="
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>

    <Grid>    
        <ComboBox ItemsSource="{Binding Path=DatabaseContext.Schools}"> //Exception here
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Label Content="{Binding Path=Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>            
    </Grid>
</Window>

ViewModel.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryingWPFWithUltimate
{
    class ViewModel:INotifyPropertyChanged
    {    
        private DatabaseContext _databaseContext;
        public DatabaseContext DatabaseContext { get { return _databaseContext; } set { _databaseContext = value; RaisePropertyChanged("DatabaseContext"); } }

        public ViewModel()
        {
            DatabaseContext = new DatabaseContext();
        }
    }
}

Here is how the entity data model looks (it is very simple) -

Model

Can anybody explain me what I am doing wrong and how I can fix this?

Joe Slater
  • 2,483
  • 6
  • 32
  • 49

1 Answers1

1

As explained in the other post you cannot databind directly to DbSet which is what you are attempting to do. Instead you need to get a create a local collection and populate it with the results of a query.

Here is a very good tutorial.

And an example of the view model you can start with:

using System.Collections.ObjectModel;

namespace TryingWPFWithUltimate
{
    class ViewModel:INotifyPropertyChanged
    {    
        private DatabaseContext _databaseContext;

        public ViewModel()
        {
            _databaseContext = new DatabaseContext();
            Schools = new ObservableCollection<School>(_databaseContext.Schools);
        }

        public ObservableCollection<School> Schools { get; set; }

    }
}

And the binding:

<ComboBox ItemsSource="{Binding Path=Schools}">
Dave Williams
  • 2,166
  • 19
  • 25
  • Ok. But do you know if there is a way to make Entity Framework generate ObservableCollection instead of DbSet? Or if there is a way you can do in XAML like this - {Binding Path=DatabaseContext.Schools.ToList()} ? – Joe Slater Jul 01 '13 at 15:06
  • Definitely not to the second one. You could fiddle around with your context to generate observable collections but it would be a really bad idea. What is wrong with doing it this way? It is very simple and doesn't require any hacky solutions! – Dave Williams Jul 01 '13 at 15:30
  • Do you know if I can use a ValueConverter to convert DbSet to ObservableCollection? – Joe Slater Jul 01 '13 at 15:33
  • Yes you probably could, however it would be much, much more complicated that just using the standard way (as described in the article I linked you to). It would be very bad practice and it would violate many of the good reasons to use things like MVVM, databinding etc. Can you answer me why you are so aversed to using the method above? – Dave Williams Jul 01 '13 at 15:45
  • Because I just have way too many entities and each entity has way too many properties. I do not want to be rewriting all the properties and entities all over again in ViewModel. – Joe Slater Jul 01 '13 at 16:09