Reputation: 810
Using "Microsoft.Azure.DocumentDB": "1.5.2" I am having a problem with complex objects correctly persisting to the database. Their initial storage seems to go OK, but a Upsert or Replace seems to not correctly update the Document when there are changes only to the deeper parts of a complex structure.
I have a object that looks like this (somewhat altered, cut down version)...
public class Profile : Document {
public Profile()
{
Id = Guid.NewGuid().ToString();
}
[JsonProperty(PropertyName = "userId")]
public string UserId { get; set; }
[JsonProperty(PropertyName = "defaultContext")]
public Guid DefaultContext { get; set; }
[JsonProperty(PropertyName = "contexts")]
public Dictionary<Guid,UserContext> Contexts { get; set; }
}
}
And the UserContext looks like this...
public class UserContext
{
[JsonProperty(PropertyName = "flag")]
public Boolean Flag { get; set; }
[JsonProperty(PropertyName = "stuff")]
public List<String> Stuff { get; set; }
}
The idea is that a profile can have multiple contexts but there is a default one. This allows me to say something like...
userProfile.Contexts[userprofile.DefaultContext].Flag = true;
The problem is, assuming that one of these lives in an Azure DocumentDB, if I change "flag" in the context neither ReplaceDocumentAsync(userProfile) nor UpsertDocumentAsync(collectionUri, userProfile) actually seem to persist the changes to the database.
For instance in a simple example like this...
userProfile.Contexts[userprofile.DefaultContext].Flag = true;
var result = await Client.ReplaceDocumentAsync(userProfile);
I can look at userProfile in the debugger and see that Flag is set to true. Then if I look at result, Flag will still be false (it's value in the DB before operation).
I have tried this with "strong" and "session" consistency flags to no avail. I feel like there is something in the definition of the class that is keeping it from serializing correctly through to the DocumentDB, but I am unsure what that may be.
Any ideas welcome :)
Ken
Upvotes: 3
Views: 2097
Reputation: 2728
This is a weird serialization issue with JSON.NET. Document, that you are extending is a Dynamic object (meaning you can at runtime add new properties to an object among other things). JSON.NET has an interesting way of handling serialization when mixing Dyanmic and static objects like Dictionary<>
The best way to fix this is to not extend from Document and use simple POCOs. As soon as you do that, the problem will go away.
Change your class to: public class Profile : { ... }
Upvotes: 4