Ask a questionAsk a question
 

QuestionWhy doesn't tracked entity send update request

  • Tuesday, October 27, 2009 6:35 PMSkeeveSixtyNine Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Here there,

    I am wondering if anyone can explain this to me. I have an entity that is tracked by the context using latest 1.5 CTP 2, but when I modify a value of the property and then call BeginSaveChanges, no updates are sent to server.  However, if I do an UpdateObject first - it works just fine.

    It doesn't make sense to me because I really thought that the context would know when a tracked entity is modified and will therefore save it appropriately.

    I can give code, but I figure plain language might be easier for now. :-)

    Thanks!

All Replies

  • Wednesday, October 28, 2009 11:34 AMVitek Karas - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Tracking a certain entity for the context means that the context knows about the instance and keeps some metadata for it (like it's URL for example). It doesn't involve itself in databinding, which is what would be able to tell the context that the instance has changed.
    So using just the DataServiceContext you need to call the UpdateObject to notify the context that the instance has changed.

    If you on the other hand use databinding feature, this will work seamlessly.
    There's a blog post about this feature here: http://blogs.msdn.com/astoriateam/archive/2009/09/01/introduction-to-data-binding-in-ctp2.aspx
    The DataServiceCollection<T> uses the databinding interfaces and thus it gets notified if an entity instance. which is in the collection, changes. It will then call the UpdateObject on the underlying context for you.

    Thanks,
    Vitek Karas [MSFT]
  • Wednesday, October 28, 2009 3:04 PMDave Hennessy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi!
    Thanks for your response. I appreciate your help.

    Our partial entity class is defined this way in the generated code:

    public partial class ItnEntity : global::System.ComponentModel.INotifyPropertyChanged

    And when we create an ItnEntity, we do so by:

    var itnEntity = new ItnEntity() {set stuff here};

    context.AddItnEntity(itnEntity);

    context.Save();

    This works and the data are saved to db as expected. Shouldn't that itnEntity be tracked now now so that when it is updated, UpdateObject() is automatically run? This is specifically what my test does, but the second time save is run after changing the entity fields, the entity is not saved (UNLESS I run UpdateObject() manually)

    Thanks again!
  • Wednesday, October 28, 2009 3:07 PMSkeeveSixtyNine Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sorry! Someone else was using my computer and logged in. Then I submitted my post in another window and it was under his account. So, the above message is from me, not Dave. :-P
  • Thursday, October 29, 2009 10:38 AMVitek Karas - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    It is the expected behavior - as I described above.
    What you want is called "data binding". It means that when a property of a given object changes something else reacts to this change (for example by calling UpdateObject).

    The DataServiceContext class doesn't support/use data binding.
    The DataServiceCollection<T> class does.

    So if you're only using the DataServiceContext class you need to call methods to let it know an entity has changed. That's what you do when you call AddItnEntity (which will call AddObject) to add an entity. And the same way you need to call UpdateObject if the entity changes.

    If you use DataServiceCollection<T> the class will do this for you - that is if you add an item to the collection it will call AddObject on it. It also registers itself as an observer on all items in the collection (using the INotifyPropertyChanged interface on the entity class) and thus will get notified when the entity instance changes. It will react to that by calling UpdateObject.
    DataServiceContext doesn't register itself as an observer (it doesn't have such functionality), so it can't know about the entity instance changing.

    Please refer to this blog post for details about the DataServiceCollection<T> class and the "data binding" support we have: http://blogs.msdn.com/astoriateam/archive/2009/09/01/introduction-to-data-binding-in-ctp2.aspx

    Thanks,
    Vitek Karas [MSFT]