locked
Problem updating related entity via AJAX RRS feed

  • Question

  • Having troubles updating an entity relationship via Ajax.  I have a table (call it Customers) which is related to a CustomerType table, which is just a basic lookup table.  Back end is a DataService with EDM auto generated model. I am loading the a customer via ajax, then trying to change the CustomerType via an update.  Basically just trying to change the foreign key value on the Customer table.

    After a lot of trial/error, inspecting traffic with Fiddler, and looking at this post, I got to something like this:

    dataService.query('/Customers(4)', function(customer, context, operation){

    customer.CustomerType = { __metadata: { uri: 'CustomerType(3)' } };

    dataService.update(customer, 'Customers(4)');    // *BANG*
    });

    This throws the folowing:

    Microsoft JScript runtime error: Sys.Data.DataServiceFailedException: The data operation 'update' returned invalid data. The JSON wrapper is incorrect.


    Changing scalar properties works fine in the above scenario.

    Interestingly, if I query the customers with the CutomerType expanded, all updates blow up with the same error.  Something like:

    dataService.query('/Customers(4)?$expand=CustomerType', function(customer, context, operation){ 

    customer.Name = "The Big Lebowski";     // works fine if CustomerType is NOT expanded
    dataService.update(customer, 'Customers(4)');  // *BANG*
    });

    Is this a bug?  Has anybody done something like this with success?  
    Thursday, September 25, 2008 11:03 AM

All replies

  • No solution to this yet, but I've spent a bit of time with fiddler, service trace viewer and sql profiler...  At this point I can a couple of things...

    1)  The service itself is throwing a CommunicationObjectAbortedException
    2) the database is successfully executing a select call on the main table sometime before the service throws its exception

    If I make a valid update request (by only changing a scalar property and leaving the navigation prop alone), the database is getting the same select call on the main table, then executing an update call as expected.

    Probably no news here, and the exception from the service doesn't illuminate much (to me) at this point.  Seems like maybe the service is executing the first select to get the record which needs to be updated, then is maybe trying to execute a second select to get the new value of the navigation property that I want to modify.  At this point something ugly is happening, and its all going south. Kinda makes me think that the problem is server side, and nothing much to do with the request itself. I'd really appreciate any feedback on this....
    Thursday, September 25, 2008 4:28 PM
  •  

    Here's the forum that tells how to deal with relationships in Astoria client: http://blogs.msdn.com/phaniraj/archive/2008/07/02/working-with-relations-in-ado-net-data-services-beta-1.aspx

     

    Let me know if you have more questions.

     

    Thanks

    Pratik

    Tuesday, September 30, 2008 10:54 PM
    Moderator
  • Thanks for the reply Pratik.  I've actually read that one a few times, and can understand how all that works in relation to the .net client.  Trouble is I can't make this work via the AJAX client.  Doesn't seem like there is any support for adding the links retroactively.  I've tried a number of ways to get around it by fiddling with the __metadata for foreign key properties, but no joy.  I can sucessfully INSERT new items with the correct foreign keys by doing something like:

    myObj.MyFKProp = {__metadata: {uri: 'theResourceUri' } };

    but updates flop....

    I just hope I'm not missing something really obvious.

    Cheers,
    /brettman
    Monday, October 6, 2008 12:43 PM
  •  

    First, POST (insert) supports deep inserts - in other words, you can insert a new entity and along with that, you can insert a bunch of other related entities (at any depth). In your example, you can insert Customer and along with it, you can also insert CustomerType, which is a related entity.

     

    But for MERGE/PUT (updates), deep updates are not supported. Hence you can only update top level entity. You can update the links, since theoritically, they are updates to the top level entity itself. For example, if you updating a customer, you can update any of its field (scalar or complex property fields), but you can update the content of CustomerType, which is a related entity. Also while updating a customer, you can change the value of the CustomerType (this is what i meant by updating the link), but you can update the content of the existing CustomerType entity.

    Hence you are seeing errors whenever you query using the expand option, and then try updating it.

     

    Can you please send us the exact payload and response in the first case using fiddler?

     

    Thanks

    Pratik

     

    Monday, October 6, 2008 5:04 PM
    Moderator
  •  

    Ok, what I'm trying to do is just update the CustomerType on the Customer. Effectively just want to change the FK value in the Customers table in the db.

     

    Unfortunately, I'm just seeing this post as I get home, so will have to wait for the morning to get the fiddler output.

    Monday, October 6, 2008 5:23 PM