Skip to content

DbSet<>.Local InvalidOperationException with ListBox.ItemsSource #7397

@ahoischen

Description

@ahoischen

Using the DbSet<>.Local property as the source for an System.Windows.Controls.ListBox and then adding and removing an instance causes an InvalidOperationException to be thrown. This is not an external issue as this is following your recommended use case for this property.

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=Collection Remove event must specify item position.
  Source=PresentationFramework
  StackTrace:
       at MS.Internal.Data.EnumerableCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
       at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
       at Microsoft.EntityFrameworkCore.ChangeTracking.LocalView`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
       at Microsoft.EntityFrameworkCore.ChangeTracking.LocalView`1.StateManagerChangedHandler(InternalEntityEntry entry, EntityState previousState)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.LocalViewListener.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges)
       at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges)
       at Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry.set_State(EntityState value)
       at Microsoft.EntityFrameworkCore.DbContext.Remove[TEntity](TEntity entity)
       at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Remove(TEntity entity)
       at EntityFramework_bug_local_wpf_listbox.MainWindow.Window_Loaded(Object sender, RoutedEventArgs e) in C:\Users\psych\Documents\git\entityframework-bug-local-wpf-listbox\view\MainWindow.xaml.cs:line 39
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
       at System.Windows.BroadcastEventHelper.BroadcastLoadedEvent(Object root)
       at MS.Internal.LoadedOrUnloadedOperation.DoWork()
       at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
       at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
       at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
       at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
       at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
       at System.Windows.Interop.HwndTarget.OnResize()
       at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
  InnerException: 

Steps to reproduce

I have created an example solution with minimal setup here but have removed the repo since this issue has been fixed. However I can also quickly summarize the process:

  • Create a context
  • Set the ItemsSource property of a ListBox to a local view of one of that context's tables.
  • Add an object to that table
  • Remove that object

The final step results in the exception above.
Example class:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var context = new FailingModel();
        _listBox.ItemsSource = context.Customers.Local;
        var customer = new Customer();
        context.Customers.Add(customer);

        // The following line throws an InvalidOperationException
        context.Customers.Remove(customer);
    }
}

Further technical details

EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.Sqlite 1.1.0
Operating system: Windows 10 Home x64
IDE: Visual Studio 2015

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions