A first chance exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll

entity-framework exception invalidoperationexception wpf



DataBase First Entity Framework v4.4 is what I'm utilizing. Table1 in the database (and data model) is related to Table2 in a 1:many manner.

In WPF, I'm connecting a DataGrid to Table1.Local. First(). Table2 (for simplicity, assume that there is an entity in Table1.Local to begin with).


Public SomeEntityDBContextWithTable1AndTable2 Container { get; set; }
Public ICollection Table2ToDisplay { get { return Container.Table1.Local.First().Table2; } } //Note: :Many navigation properties return ICollection<T>, but the object type is of ObservableCollection<T>.

I have the following in XAML.

<GroupBox Header=Table2 DataContext="{Binding Path=Table2ToDisplay, UpdateSourceTrigger=PropertyChanged}">
    <DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False" CanUserAddRows="True">
            <!--A bunch of columns-->

I receive the System.InvalidOperationException occurring in PresentationFramework.dll when clicking inside a NewItemPlaceHolder that just so happens to be a text box. My application is not crashed by this, but I can see it in the output. My best bet is that the CollectionChanged event fires on another thread since entities are added on a different thread, which results in the InvalidOperationException. However, since the majority of the code is written using XML, I can't seem to handle this issue (or perhaps it is already handled; it's just being reported to Output). Is it possible to utilize EntityFramework safely when ":Many" navigation properties are of type ObservableCollection? CanUserAddRows="True"

It's important to note that despite my efforts, the output still contains the InvalidOperationException. I've tried enclosing my Table2ToDisplay property inside a CollectionViewSource.

I appreciate it.

3/14/2013 4:14:44 PM

Accepted Answer

Okay. Making the debugger in Visual Studio pause at all exceptions gave me some useful information. My Entity's PropertyChanged event caused the exception, which read "NewItemPlaceHolder is not allowed during an AddNew..." I seem to be okay because it turned out that this exception was already being handled.

3/14/2013 4:48:08 PM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow