Batch Requests with Insert/Merge RRS feed

  • Question

  • I have a List

    List<Id> ids

    which contains list of 50 ID's. I was wondering what is the best way to do a batch request using Insert?merge.

    I have tried this and doesn't work

    serviceContext.AttachTo("Id", ids)



    What is the best way to achieve the above ?

    Sunday, September 9, 2012 7:39 AM


All replies

  • Can you describe the structure of your "Id" class (I'm assuming it's a class and derives from TableServiceEntity)? For entity batch transaction to work, all items in the batch must have same PartitionKey and the total number of items in a batch must be less than 100.

    Once you have that, you would need to loop through your "ids" collection and attach each object separately. Something like:

    foreach (var id in ids)
        serviceContext.AttachTo("Id", id);
    serviceContext.SaveChanges(SaveChangesOptions.ReplaceOnUpdate | SaveChangesOptions.Batch);
    Hope this helps.

    Sunday, September 9, 2012 1:14 PM
  • Hello Gaurav,

    When I try the above, and on second iteration AttachTo throws this exception: The context is already tracking a different entity with the same resource Uri.

    Thanks for your help.


    Sunday, September 9, 2012 5:57 PM
  • Take a look at MergeOptions property of TableServiceContext: http://msdn.microsoft.com/en-us/library/system.data.services.client.mergeoption.aspx.

    Try your code with "NoTracking" merge option. This should be defined where you're creating an instance of your table service context object.

    Hope this helps.

    • Marked as answer by thukralz Monday, September 10, 2012 7:25 AM
    Monday, September 10, 2012 4:09 AM
  • I added this line just before the foreach loop

    this.serviceContext.MergeOption = System.Data.Services.Client.MergeOption.NoTracking;

    It still gives the same error. 
    Monday, September 10, 2012 5:57 AM
  • Can you post your entire code here? This might help. In my project, I added this option right after I created an instance of table service context.
    Monday, September 10, 2012 6:25 AM
  • The code has started working partially, I was not inheriting from TableServiceContext and instead adding partitionkey, rowkey and timestamp manually. After removing those and inheriting from TableServiceContext, it has moved to next step.

    Now when I try to save, I get this error:


    "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\r\n<error xmlns=\"\r\n">http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">\r\n  <code>ResourceNotFound</code>\r\n  <message xml:lang=\"en-US\">0:The specified resource does not exist.</message>\r\n</error>"

    I already have the table and it has lot of rows. I have verified the table name and getting rows from this table works. I'm using Development Storage.

    Monday, September 10, 2012 6:55 AM
  • I think batch upsert (Insert or Replace/Insert or Merge) functionality still does not work in development storage (at least I was not able to make it work there :)). Try using it in cloud storage.
    Monday, September 10, 2012 6:58 AM
  • I don't have azure account yet, I will create one and upload it soon. In the meantime, I'm going to mark your response as "answer". Thanks for your help.


    Monday, September 10, 2012 7:25 AM