Reputation: 1
I was using Cosmonaut in build library function to convert the c# objects to cosmosDb document like this:
var document = fakeUser.ConvertObjectToDocument();
Now I want to remove the cosmonaut library. Is there any other way to convert c# objects to cosmosdb documents?
Upvotes: 0
Views: 1540
Reputation: 7200
Simply copy Cosmonaut's code for the method and remove what you don't need. The following will work.
public static Document ConvertObjectToDocument<TEntity>(this TEntity obj) where TEntity : class
{
var dynamicDoc = JsonConvert.DeserializeObject<dynamic>(JsonConvert.SerializeObject(obj));
using (JsonReader reader = new JTokenReader(dynamicDoc))
{
var document = new Document();
document.LoadFrom(reader);
return document;
}
}
Upvotes: 2
Reputation: 15621
To write a C# class (called yourClass
) to DocumentDB as a document, simply do something like this:
await _client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), yourClass);
You can find more information on working with DocumentDB here: Azure Cosmos DB: SQL API getting started tutorial, and more specifically Step 6: Create JSON Documents.
Upvotes: 1