locked
Why no SaveChanges, only SavingChanges on the Context RRS feed

  • Question

  • I've had a go at adding some new save methods on top of the context but these don't hook up correctly.

            public event EventHandler SavedChanges;
            public new int SaveChanges()
            {
                return base.SaveChanges(true);
            }
    
            public new int SaveChanges(bool acceptChangesDuringSave)
            {
                int ret = base.SaveChanges(acceptChangesDuringSave);
    
                if (SavedChanges != null) SavedChanges(this, null);
    
                return ret;
            }

    What I would like to do is run something after the data has been saved to the database.  I need to fire off an external process to do some processing and it will use other means to connect to the database so the data needs to be there.

    Thanks for your help
    Monday, May 4, 2009 10:14 AM

Answers

  • Unfortunately, after talking to some other team members and scouring the forums, it looks like this is a known and expected behavior.

    http://social.msdn.microsoft.com/forums/en-US/adodotnetdataservices/thread/944d8b1a-cbeb-4501-98d3-332fbb587108/

    As suggested in that thread, it looks like the only real option is to reimplement the IUpdatable used by ADO.NET Data Services to make updates on the ObjectContext.

    You may want to bring the topic up in the EF forums, and find out if there is any planned support for a post-SaveChanges event on the base ObjectContext in a future release.

    Apparently, ADO.NET Data Services and the EF both provide hooks that fire before SaveChanges, but not after.

    I can propose this as a possible future addition to ADO.NET Data Services, but I can't make any promises about when/if it would be supported.
    Matt Meehan, ADO.NET Data Services (Astoria)
    Tuesday, May 5, 2009 12:42 AM
    Moderator

All replies

  • Hi,

    Based on the code above, I'm guessing that you're trying to change the behavior of the Entity Framework's ObjectContext, and not the DataServiceContext used by the ADO.NET Data Services client.

    Are you trying to expose a modified object context as a Data Service, and having difficulty getting your new methods to be called?

    Otherwise, if this is purely an EF issue, then we can move the thread to their forums instead.
    Matt Meehan, ADO.NET Data Services (Astoria)
    Monday, May 4, 2009 3:43 PM
    Moderator
  • Indeed, that is for the ObjectContext and maybe the title would be better suited to the EF forum.  I'm using Data Services on top of the EF, and I need a point where I can hook up a process following the data being saved.  If Data Services has no point then this post should be moved to the EF forum I guess.

    Monday, May 4, 2009 9:29 PM
  • So the EF presumably generated a set of classes for you based on your model, and that code contained a subclass of ObjectContext that was specific to your model.

    Did you add your new methods to that code? If so, were they added in a partial class or directly to the generated file? (I ask because re-generating the file could result in those methods being lost).

    What does your data service class look like? I'm guessing you have something like: MyService : DataService<MyModel> where 'MyModel' is the name of the class the EF generated. Is this true?

    My understanding is that if you create a DataService<T> and T is your modified ObjectContext subclass, then things should work the way you're hoping.

    Also, I would recommend writing a simple application using just the EF, just to make sure your events are firing correctly without Data Services in the picture.
    Matt Meehan, ADO.NET Data Services (Astoria)
    Monday, May 4, 2009 11:53 PM
    Moderator
  • That's right, I have a generated model that produced some code ...

    public partial class MyEntities : global::System.Data.Objects.ObjectContext
    
    And I've tried a number of things; one was to put the SaveChanges "new" methods in a partial by the same name, if I call SaveChanges myself then it works fine but Data Services does not call them.  The other was to sub class the ObjectContext and change the generated code to use that.  e.g.

    public partial class MyEntities : MyObjectContext
    
    public class MyObjectContext : global::System.Data.Objects.ObjectContext
    {
      public int SaveChanges...
    }
    Still no joy, it seems like Data Services does not call the SaveChanges for whatever reason, maybe something to do with the way it's instantiated?

    I'll try and build a simple model as suggested to see if the events are firing.



    Tuesday, May 5, 2009 12:13 AM
  • It seems to me that either way should work, provided your data service class refers to the right class. IE, your class should inherit from DataService<MyObjectContext> or DataService<MyEntities>. The <T> that is specified should be a class that has your new implementation of SaveChanges.

    In the meantime, I'll try to follow up with the team and see if this is a known issue or if there's something we're missing.
    Matt Meehan, ADO.NET Data Services (Astoria)
    Tuesday, May 5, 2009 12:19 AM
    Moderator
  • Unfortunately, after talking to some other team members and scouring the forums, it looks like this is a known and expected behavior.

    http://social.msdn.microsoft.com/forums/en-US/adodotnetdataservices/thread/944d8b1a-cbeb-4501-98d3-332fbb587108/

    As suggested in that thread, it looks like the only real option is to reimplement the IUpdatable used by ADO.NET Data Services to make updates on the ObjectContext.

    You may want to bring the topic up in the EF forums, and find out if there is any planned support for a post-SaveChanges event on the base ObjectContext in a future release.

    Apparently, ADO.NET Data Services and the EF both provide hooks that fire before SaveChanges, but not after.

    I can propose this as a possible future addition to ADO.NET Data Services, but I can't make any promises about when/if it would be supported.
    Matt Meehan, ADO.NET Data Services (Astoria)
    Tuesday, May 5, 2009 12:42 AM
    Moderator
  • Ah, I see.  I'll propose to the EF people to add it.  It may be useful in DS as well.

    Thanks for your help Matt.
    Tuesday, May 5, 2009 2:49 AM