I'm seeing some weird behavior saving to DocumentDB. I started out saving documents using a plain old class that looked like this:
public class Person
{
public string Name;
public int Age;
}
I saved these documents like this:
var person = new Person { ... };
client.CreateDocumentAsync(myCollectionLink, person);
This worked fine. Properties were saved with exactly the names in the class. Then I realized I needed the document's SelfLink in order to perform updates and deletes. "Ah," I thought. "I'll just derive from Document, like so:
public class Person: Microsoft.Azure.Documents.Document
{
public string Name;
public int Age;
}
However, much to my surprise, when I made this change, new documents were created completely blank, except for the "id" property assigned by DocumentDB itself.
I double-checked multiple times. Deriving from Document prevents my custom properties in the document from being saved...
...unless I explicitly decorate each one with [JsonProperty], like so:
public class Person: Document
{
[JsonProperty(PropertyName="name")]
public string Name;
[JsonProperty(PropertyName="age")]
public int Age;
}
Then it works again (using, of course, the new more JSON-appropriate camelCase property names). And, upon retrieval, the objects get populated with the SelfLink property that I need for updates and deletes. All good.
By my questions are...Why did this happen? Am I doing something wrong by deriving from Document? Your feedback would be much appreciated.
There are two ways you can connect to documentDB from an Azure function. DocumentClient documentClient = new DocumentClient( "SERVICE_ENDPOINT", "MASTER_KEY", ConnectionPolicy. GetDefault(), ConsistencyLevel. Session);
Azure Cosmos DB is Microsoft's globally-distributed, multi-model database service "for managing data at planet-scale." It builds upon and extends the earlier Azure DocumentDB, which was released in 2014. It is schema-less and generally classified as a NoSQL database.
Azure DocumentDB is a schema-free NoSQL document database service that delivers faster reads and writes time by indexing all documents without the need for schema and provides on demand database scalability.
This behavior is attributed to how JSON.NET deals with properties on dynamic objects. It effectively ignores them unless you decorate them with the JsonProperty attribute.
You can either work with plain POCO or you can extend from Resource (shown below), which is a static object that Document itself extends.
public class Person: Microsoft.Azure.Documents.Resource
{
public string Name;
public int Age;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With