locked
Cancel changes in DataServiceCollection RRS feed

  • Question

  • Hi, not quite sure where to put this but here goes.

    I have the following code in my WPF client connecting to a DataService:


            private EPODServiceReference.EPODEntities dsc = 
                new EPODServiceReference.EPODEntities(new Uri(Properties.Settings.Default.EPODServiceUri));
    
            private DataServiceCollection<EPODServiceReference.DLN_DeliveryNotes> dln_DataServiceCollection;
    
    
    
            dln_DataServiceCollection =
                    new DataServiceCollection<EPODServiceReference.DLN_DeliveryNotes>
                        (from d in dsc.DLN_DeliveryNotes
                             .Expand("COM_Companies")
                             .Expand("DRV_Drivers")
                         orderby d.DLNReference
                         select d);
    
                dataGrid1.ItemsSource = dln_DataServiceCollection;


     

    Running the app I can edit and save the changes back to my db and when the user wants to 
    cancel the unsaved changes in the DataServiceCollection I just run

    dln_DataServiceCollection =
                    new DataServiceCollection<EPODServiceReference.DLN_DeliveryNotes>
                        (from d in dsc.DLN_DeliveryNotes
                             .Expand("COM_Companies")
                             .Expand("DRV_Drivers")
                         orderby d.DLNReference
                         select d);


    again and the code executes but here is my confusion.

    dln_DataServiceCollection still has the changes that was not saved?
    Where are the changes being tracked and how can I cancel then?

    Thanks.
    • Moved by Mog Liang Monday, March 8, 2010 8:59 AM From: Windows Communication Foundation (From:Windows Communication Foundation)
    Friday, March 5, 2010 1:07 PM

Answers

  • Hi,

    The changes are tracked in DataServiceContext object (in your application this class is usually derived from and is your main context class to which you specify the URL of the service to talk to). You can easily cancel changes by "forgeting" about the context. Usually the idea is that you create the context instance for each standalone operation, if you need to cancel the operation you simply forget about the context instead of calling SaveChanges.
    Note though, that if you remember entity instances accross these operations, the entites are only tracked as long as the context which created them exists. If you forget about the context, the entities still exist and you can work with them, but the new context will not know about them (and thus will not allow you to modify them). To overcome this you can either re-request the entities from the server (you will get a new instance of the entity to work with), or as the last resort you can take a look at AttachTo method on the context.
    If you can't forget about the context, you could use Detach and AttachTo to force the context to forget about the entity, and then add it again (this will effectively mark it as unchanged).

    But the suggested solution if at all possible would be to scope your changes by using new instances of the context class. That way you either call SaveChanges to persist the changes, or you forget about the context class and that will effetively revert/cancel the changes.

    Thanks,
    Vitek Karas [MSFT]
    • Marked as answer by Louw Grobler Monday, March 8, 2010 11:17 AM
    Monday, March 8, 2010 11:03 AM
    Moderator