NotSupportedException:直接绑定到商店查询的数据(DbSet,DbQuery,DbSqlQuery)

时间:2013-07-01 14:34:36

标签: c# .net wpf entity-framework

我收到以下异常

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

堆栈追踪:

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

以下是实体数据模型的外观(非常简单) -

Model

有人可以解释一下我做错了什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

正如在另一篇文章中所解释的那样,你不能直接数据绑定到DbSet,这是你试图做的。相反,您需要创建一个本地集合,并使用查询结果填充它。

这是一个非常好的tutorial

您可以从以下开始的视图模型示例:

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

    }
}

和绑定:

<ComboBox ItemsSource="{Binding Path=Schools}">