Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

MVVM, DataGrid, CollectionViewSource doesn't save with EntityFramework

$
0
0

I'm having trouble saving from a MVVM Datagrid to EntityFramework. Here's the relevant code...

// members in my ViewModel...
 public CollectionViewSource MyEntitys { get; set; }
 private ObservableCollection<MyEntity> MyEntitysInternal { get; set; }

// in my ViewModel constructor...
 MyEntitysInternal = repository.MyEntitys().Local;
 MyEntitys = new CollectionViewSource();
 MyEntitys.Source = this.MyEntitysInternal;

// XAML
<DataGrid x:Name="MyEntityDataGrid"
    AutoGenerateColumns="False" 
    CanUserAddRows="True"
     CanUserDeleteRows="True"
     IsReadOnly="False"
     SelectionMode="Single"
     SelectionUnit="FullRow"
     ItemsSource="{Binding MyEntitys.View, UpdateSourceTrigger=PropertyChanged}"
     SelectedItem="{Binding Path=CurrentMyEntity, Converter={StaticResource ignoreNewItemPlaceHolderConverter}}"
  IsSynchronizedWithCurrentItem="True">

Note: IgnoreNewItemPlaceholderConverter comes from here to avoid a binding issue:   http://blog.spencen.com/2009/04/30/problems-binding-to-selectedvalue-with-microsoftrsquos-wpf-datagrid.aspx
 
My problem is when I add a new row in the datagrid, I see an object in the ObservableCollection, but I can't tell programmatically if it's a new object and entities.SaveChanges() doesn't write it to SQL.

I hooked a CollectionChanged event to the ObservableCollection (see this thread http://social.msdn.microsoft.com/Forums/en-US/d6777acb-a262-4096-8e6e-d270cb0670c8/ef5-dbcontextsavechanges-datagrid-saves-updates-but-no-inserts-and-deletes), but it fires as soon as the user types in a single cell even when I set UpdateSourceTrigger=LostFocus, so the collection has no items. This seems to be more of WPF issue than an EF issue.

Any advice what I'm doing wrong?

Many Thanks!


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>