none
Reflecting deleted objects (not saved) in WPF bindings RRS feed

  • Question

  • I am using the standard ADO.NET POCO Entity Generator template with an EF4 model. I want my users to be able to flag an object for deletion and later save that change - that part is working fine. However, the ObjectSet collections don't reflect that the object is deleted until after I save my changes to the database. How can I force it to refresh these collections with the current state such that any objects being deleted will not be present?
    Friday, June 17, 2011 5:03 PM

All replies

  • Hi,

    It's a bit difficult to answer without knowing your code, but if you mark the POCO entity as deleted in the context.ObjectStateManager some way (eg, by calling ObjectSet.DeleteObject), you can safley just remove it from the collection afterwards, you don't need to have a track of it there anymore.

    BUT, if you're binding directly to the ObjectSet, and not a result from it (ie. ToList() or similar) you can't hide them, the ObjectSet is not a manipulative collection.

    So, what you can try is:

    1. Create a ObservableCollection property in your code-behind and bind it to your WPF control (eg. a DataGrid)
    2. Fetch the data from your ObjectSet into the ObservableCollection (eg. MyCollection = new ObservableCollection(context.MyTable.ToList()); )
    3. When the user deletes an entity, remove it from the context.MyTable with for example context.MyTable.DeleteObject(deletedEntity);
    4. Then remove the entity from your ObservableCollection by calling MyContext.Remove(deletedEntity);

    When this is done you'll see that the entity will automatically be removed from your DataGrid, since it was removed from an ObservableCollection.

    Hope this helps!


    --Rune
    Saturday, June 18, 2011 6:46 AM
  • Rune,

    I've seen similar workarounds before, but hoped there was another way to do this. It's really very odd that EF doesn't handle this itself - when I think "I'm binding to this list of objects" and I've abstracted away the database logic, I would expect to be able to tell it to delete an object and have that change show up. Is there any particular reason why deletes can't be reflected locally without the developer having to do the work?

    Is there perhaps a T4 template available that handles abstracting this away? I really don't want to go through and change every single data binding within my code for this.

    Tuesday, June 21, 2011 6:05 PM
  • I was able to modify the existing T4 POCO template to provide this functionality, but it a'int pretty. It involved creating a private ReadOnlyObservableCollection in addition to the existing ObjectSet, modifying the get accessor for the public property to return this ReadOnlyObservableCollection (and to set it if the object was null) and basically foregoing most of the advantages of lazy loading. The ladder logic for determining what type of object is being added/removed alone is pretty painful to look at - I can't imagine that it is very efficient at all. I'll be looking for something a little more efficient.

     

    Tuesday, June 21, 2011 10:19 PM
  • What you could do is to take a look at the Self-Tracking Entity template. It gives each entity a change tracker that keeps the track of the current entity status.

    The objects isn't that much of clean POCO entities then, but it is an option.


    --Rune
    Wednesday, June 22, 2011 5:47 AM