C# 将列表框绑定到数据库表

C# 将列表框绑定到数据库表,c#,wpf,entity-framework,linq,xaml,C#,Wpf,Entity Framework,Linq,Xaml,我正在构建一个WPF应用程序。我首先使用实体框架代码创建了数据库,现在我想在GUI中使用我的数据库。 我在xaml中有一个列表框 XAML <Window.Resources> <DataTemplate x:Key="lstDishesTemplate"> <StackPanel> <StackPanel Orientation="Horizontal">

我正在构建一个WPF应用程序。我首先使用实体框架代码创建了数据库,现在我想在GUI中使用我的数据库。 我在
xaml中有一个列表框

XAML

<Window.Resources>
        <DataTemplate x:Key="lstDishesTemplate">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path = Dishes.Description}" FontWeight="Bold"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
<ListBox x:Name="lstDishes" Height="415" VerticalAlignment="Bottom" Margin="0,0,1032,42" HorizontalAlignment="Right" Width="281" ItemTemplate="{StaticResource lstDishesTemplate}" Background="#FFDDC9B0" BorderBrush="{x:Null}"/>
我得到一个例外:

EntityFramework.SqlServer.dll中发生类型为“System.Reflection.TargetInvocationException”的未处理异常

附加信息:由呼叫目的地生成的异常(已翻译)

更新:

System.Reflection.TargetInvocationException was unhandled
  HResult=-2146232828
  Message=Eccezione generata dalla destinazione di una chiamata.
  Source=mscorlib
  StackTrace:
       in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       in System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap(Translator translator, Type elementType, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
       in System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
       in System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
       in System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
       in System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
       in System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       in System.Collections.ObjectModel.ObservableCollection`1.CopyFrom(IEnumerable`1 collection)
       in System.Collections.ObjectModel.ObservableCollection`1..ctor(IEnumerable`1 collection)
       in Cw2.ServerWin.Window_Loaded(Object sender, RoutedEventArgs e) in C:\Users\Fennec\Documents\assessment2\Cw2\Server.xaml.cs:riga 56
       in System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       in System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       in System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       in System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       in System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
       in System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
       in MS.Internal.LoadedOrUnloadedOperation.DoWork()
       in System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
       in System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
       in System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       in System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
       in System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
       in System.Windows.Interop.HwndTarget.OnResize()
       in System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
       in System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       in System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       in System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       in MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  InnerException: 
       HResult=-2146233079
       Message=The class 'Cw2.Dish' has no parameterless constructor.
       Source=EntityFramework
       StackTrace:
            in System.Data.Entity.Core.Objects.DelegateFactory.GetConstructorForType(Type type)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Emit_ConstructEntity(EntityType oSpaceType, IEnumerable`1 propertyBindings, Expression entityKeyReader, Expression entitySetReader, TranslatorArg arg, EntityProxyTypeInfo proxyTypeInfo)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(EntityColumnMap columnMap, TranslatorArg arg)
            in System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg)
            in System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap[T](ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
       InnerException: 
System.Reflection.TargetInvocationException未处理
HResult=-2146232828
Message=在基亚马塔的目的地生成一代人。
Source=mscorlib
堆栈跟踪:
在System.RuntimeMethodHandle.InvokeMethod(对象目标、对象[]参数、签名符号、布尔构造函数)中
在System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal中(对象obj、对象[]参数、对象[]参数)
在System.Reflection.RuntimeMethodInfo.Invoke中(对象obj、BindingFlags invokeAttr、绑定器Binder、对象[]参数、CultureInfo区域性)
在System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap中(Translator-Translator,Type elementType,ColumnMap ColumnMap,MetadataWorkspace,SpanIndex SpanIndex,MergeOption MergeOption,Boolean streaming,Boolean valueLayer)
在System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare中(ObjectContext上下文,DbQueryCommandTree树,类型elementType,MergeOption MergeOption,布尔流,Span Span Span,IEnumerable`1 compiledQueryParameters,别名生成器AliasGenerator)
在System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan中(可为null的`1 forMergeOption)
在System.Data.Entity.Core.Objects.ObjectQuery`1.c_u DisplayClass7.b__u6()中
在System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 Func,IDbExecutionStrategy executionStrategy,Boolean startLocalTransaction,Boolean releaseConnectionOnSuccess)
在System.Data.Entity.Core.Objects.ObjectQuery`1.c\u DisplayClass7.b\u 5()中
在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1操作)中
在System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults中(可为null`1 forMergeOption)
在System.Data.Entity.Core.Objects.ObjectQuery`1.b__0()中
在System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()中
在System.Collections.ObjectModel.ObservableCollection`1.CopyFrom(IEnumerable`1集合)中
在System.Collections.ObjectModel.ObservableCollection`1..ctor(IEnumerable`1集合)中
在Cw2.ServerWin.Window中,加载了C:\Users\Fennec\Documents\assessment2\Cw2\Server.xaml.cs:riga 56中的(对象发送方,RoutedEventArgs e)
在System.Windows.RoutedEventHandlerInfo.InvokeHandler中(对象目标,RoutedEventTargets RoutedEventTargets)
在System.Windows.EventRoute.InvokeHandlersImpl(对象源、RoutedEventArgs参数、布尔值重调)中
在System.Windows.UIElement.RaiseEventImpl(DependencyObject发送方,RoutedEventTargets参数)中
在System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)中
在System.Windows.BroadcastEventHelper.BroadcastEvent中(DependencyObject根,RoutedEvent RoutedEvent)
在System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(对象根)中
在MS.Internal.LoadedRunLoadedOperation.DoWork()中
在System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()中
在System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()中
在System.Windows.Media.MediaContext.RenderMessageHandlerCore(对象大小调整的CompositionTarget)中
在System.Windows.Media.MediaContext.RenderMessageHandler(对象大小调整的CompositionTarget)中
在System.Windows.Media.MediaContext.Resize中(ICompositionTarget resizedCompositionTarget)
在System.Windows.Interop.HwndTarget.OnResize()中
在System.Windows.Interop.HwndTarget.HandleMessage中(WindowMessage消息、IntPtr wparam、IntPtr lparam)
在System.Windows.Interop.HwndSource.HwndTargetFilterMessage中(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
在MS.Win32.hwndwapper.WndProc中(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)中
在System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调、对象参数、Int32 numArgs)中
在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、委托回调、对象参数、Int32 numArgs、委托catchHandler)中
在System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority优先级、TimeSpan超时、委托方法、对象参数、Int32 numArgs)中
在MS.Win32.HwndSubclass.SubclassWndProc中(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam)
内部异常:
HResult=-2146233079
Message=类“Cw2.Dish”没有无参数构造函数。
Source=EntityFramework
堆栈跟踪:
在System.Data.Entity.Core.Objects.DelegateFactory.GetConstructorForType(类型)中
在System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Emit\u ConstructionEntity中(EntityType-oSpaceType,IEnumerable`1 propertyBindings,Expression entityKeyReader,Expression entitySetReader,TranslatorArg,EntityProxyTypeInfo)
在System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visite中(EntityColumnMap columnMap,TranslatorArg arg)
在System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2访问者,TArgType参数)中
在System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap中(CollectionColumnMap columnMap、TranslatorArg arg、columnMap discriminatorColumnMap、Object discriminatorValue)
在System.Data.Entity.Cor中
System.Reflection.TargetInvocationException was unhandled
  HResult=-2146232828
  Message=Eccezione generata dalla destinazione di una chiamata.
  Source=mscorlib
  StackTrace:
       in System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       in System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       in System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap(Translator translator, Type elementType, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
       in System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable`1 compiledQueryParameters, AliasGenerator aliasGenerator)
       in System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__6()
       in System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
       in System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
       in System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
       in System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
       in System.Collections.ObjectModel.ObservableCollection`1.CopyFrom(IEnumerable`1 collection)
       in System.Collections.ObjectModel.ObservableCollection`1..ctor(IEnumerable`1 collection)
       in Cw2.ServerWin.Window_Loaded(Object sender, RoutedEventArgs e) in C:\Users\Fennec\Documents\assessment2\Cw2\Server.xaml.cs:riga 56
       in System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       in System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       in System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       in System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       in System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
       in System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
       in MS.Internal.LoadedOrUnloadedOperation.DoWork()
       in System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
       in System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
       in System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       in System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
       in System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
       in System.Windows.Interop.HwndTarget.OnResize()
       in System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
       in System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       in MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       in System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       in System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       in System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       in MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  InnerException: 
       HResult=-2146233079
       Message=The class 'Cw2.Dish' has no parameterless constructor.
       Source=EntityFramework
       StackTrace:
            in System.Data.Entity.Core.Objects.DelegateFactory.GetConstructorForType(Type type)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Emit_ConstructEntity(EntityType oSpaceType, IEnumerable`1 propertyBindings, Expression entityKeyReader, Expression entitySetReader, TranslatorArg arg, EntityProxyTypeInfo proxyTypeInfo)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(EntityColumnMap columnMap, TranslatorArg arg)
            in System.Data.Entity.Core.Query.InternalTrees.EntityColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslatorVisitor.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg)
            in System.Data.Entity.Core.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg)
            in System.Data.Entity.Core.Common.Internal.Materialization.Translator.TranslateColumnMap[T](ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean streaming, Boolean valueLayer)
       InnerException: 
<TextBlock Text="{Binding Path = Description}" FontWeight="Bold"/>