Guest User

Untitled

a guest
Apr 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. using Microsoft.Azure.Documents;
  2. using Microsoft.Azure.Documents.Client;
  3. using Microsoft.Azure.Documents.Linq;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9.  
  10. namespace ConsoleApp1
  11. {
  12. public class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. DocDbDocumentProof.Run().Wait();
  17. }
  18. }
  19.  
  20. public class ExtendsDocument : Document
  21. {
  22. [JsonProperty("something")]
  23. public int Something { get; set; }
  24.  
  25. [JsonProperty("partitionKey")]
  26. public string PartitionKey { get; set; }
  27. }
  28.  
  29. public static class DocDbDocumentProof
  30. {
  31. public static async Task Run()
  32. {
  33. const string db = "<database-name>";
  34. const string collection = "<collection-name>";
  35. const string databaseUri = "<database-uri>";
  36. const string authKey = "<database-key>";
  37.  
  38. var collectionUri = UriFactory.CreateDocumentCollectionUri(db, collection);
  39.  
  40. var client = new DocumentClient(
  41. new Uri(databaseUri),
  42. authKey,
  43. new ConnectionPolicy
  44. {
  45. ConnectionMode = ConnectionMode.Direct,
  46. ConnectionProtocol = Protocol.Tcp,
  47. RetryOptions = new RetryOptions
  48. {
  49. MaxRetryAttemptsOnThrottledRequests = 10,
  50. MaxRetryWaitTimeInSeconds = 10
  51. }
  52. });
  53.  
  54. var doc = new ExtendsDocument
  55. {
  56. Id = "My unique Guid",
  57. Something = 10,
  58. PartitionKey = "My unique partition key"
  59. };
  60. await client.CreateDocumentAsync(collectionUri, doc);
  61. var results = client.CreateDocumentQuery<ExtendsDocument>(collectionUri)
  62. .Where(x => x.PartitionKey == doc.PartitionKey && x.Id == doc.Id)
  63. .AsDocumentQuery();
  64.  
  65. IEnumerable<ExtendsDocument> allResults = new List<ExtendsDocument>();
  66. do
  67. {
  68. allResults = allResults.Concat(await results.ExecuteNextAsync<ExtendsDocument>());
  69. } while (results.HasMoreResults);
  70.  
  71. var myDoc = allResults.FirstOrDefault();
  72. if (myDoc == null) return;
  73.  
  74. Console.WriteLine(JsonConvert.SerializeObject(myDoc));
  75. myDoc.Something = 11;
  76. Console.WriteLine(JsonConvert.SerializeObject(myDoc));
  77.  
  78. await client.DeleteDocumentAsync(myDoc.SelfLink, new RequestOptions
  79. {
  80. PartitionKey = new PartitionKey(myDoc.PartitionKey)
  81. }); // Clean up the mess
  82.  
  83. Console.ReadLine();
  84. // You will see in console something like
  85. /*
  86. {"something":10,"partitionKey":"My unique partition key","id":"My unique Guid","_rid":"...","_self":"...","_ts":0,"_etag":"...","something":10,"partitionKey":"My unique partition key"}
  87. {"something":11,"partitionKey":"My unique partition key","id":"My unique Guid","_rid":"...","_self":"...","_ts":0,"_etag":"...","something":10,"partitionKey":"My unique partition key"}
  88. */
  89. // You may notice that the serialization of the object duplicates the 'something' and 'companyId' properties
  90. // And that the 2nd line changes the something to 11 successfully, but the 2nd occurance of it has the orignal 10 in it
  91. }
  92. }
  93. }
Add Comment
Please, Sign In to add comment