2015-08-26 4 views
5

В документах документации DocumentDB я нахожу вставку объектов C#.Вставка документов json в DocumentDB

// Create the Andersen family document. 
Family AndersenFamily = new Family 
{ 
    Id = "AndersenFamily", 
    LastName = "Andersen", 
    Parents = new Parent[] { 
     new Parent { FirstName = "Thomas" }, 
     new Parent { FirstName = "Mary Kay"} 
    }, 
    IsRegistered = true 
}; 

await client.CreateDocumentAsync(documentCollection.DocumentsLink, AndersenFamily); 

В моем случае, я получаю JSON строку из клиентского приложения и хотел бы, чтобы вставить их в DocumentDB без десериализации их. Не удалось найти примеры того, что нужно сделать.

Любая помощь искренне оценили ..

Благодаря

ответ

6

Скопировано из published .NET Sample code -

private static async Task UseStreams(string colSelfLink) 
    { 
     var dir = new DirectoryInfo(@".\Data"); 
     var files = dir.EnumerateFiles("*.json"); 
     foreach (var file in files) 
     { 
      using (var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read)) 
      { 
       Document doc = await client.CreateDocumentAsync(colSelfLink, Resource.LoadFrom<Document>(fileStream)); 
       Console.WriteLine("Created Document: ", doc); 
      } 
     } 

     //Read one the documents created above directly in to a Json string 
     Document readDoc = client.CreateDocumentQuery(colSelfLink).Where(d => d.Id == "JSON1").AsEnumerable().First(); 
     string content = JsonConvert.SerializeObject(readDoc); 

     //Update a document with some Json text, 
     //Here we're replacing a previously created document with some new text and even introudcing a new Property, Status=Cancelled 
     using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes("{\"id\": \"JSON1\",\"PurchaseOrderNumber\": \"PO18009186470\",\"Status\": \"Cancelled\"}"))) 
     { 
      await client.ReplaceDocumentAsync(readDoc.SelfLink, Resource.LoadFrom<Document>(memoryStream)); 
     } 
    } 
+0

Что такое ресурс? –

+0

@JonathanFishbein Это класс в DocumentDB .Net SDK. – AndyJ