1

I'm having trouble adding an Entity Framework entity to a ObjectContext's EntitySet automatically using the WPF 4.0 DataGrid's add functionality. Here's the setup:

DataGrid-->BoundTo-->ListCollectionView-->BoundTo-->EntitySet

When I interactively add a row to the DataGrid, the EntitySet does not have a new entity added to it. Updating the row's cell data does in fact update the bound entity's properties, however.

Any idea what I could be doing wrong?

Here is the XAML for the ListCollectionView:

<CollectionViewSource x:Key="FieldList"
    Source="{Binding DB.Fields}"
    CollectionViewType="{x:Type data:ListCollectionView}">
    <CollectionViewSource.SortDescriptions>
        <ComponentModel:SortDescription PropertyName="Name" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

1 Answer 1

1

Is there any particular reason why you are using ListCollectionView? How are you creating your ListCollectionView?

Calling CollectionViewSource.GetDefaultView( ObjectQuery<> ) yields a BindingListCollectionView. I have just run some tests and calling IEditableCollectionView.AddNew() and IEditableCollectionView.CommitNew() adds new entity to entity set as expected.

I suggest you simply bind your ObjectContext's ObjectQuery<> property to ItemsSource of a DataGrid and the default collection view will be used, ultimately giving you the behavior you expect.

Sign up to request clarification or add additional context in comments.

6 Comments

I am using a ListCollectionView so I can sort the entities in the XAML. Is there another way to do this without using a CollectionViewSource (of type ListCollectionView)?
I have also added the XAML I use to create the ListCollectionView
How about omitting CollectionViewType="{x:Type data:ListCollectionView}"? This should create a default collection view which should do the trick for you.
When I do this, I get "'System.Windows.Data.BindingListCollectionView' view does not support sorting." when I try to set the window's DataContext. This was why I originally changed to ListCollectionView.
Too bad. In this case I can only think of staying with LitsCollectionView or whatever else makes you happy and adding some manual code to your commit logic. You need to iterate through items in your collection view and explicitly add items who's EntityState is EntityState.Detached.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.