-
Type: Improvement
-
Resolution: Done
-
Priority: Blocker - P1
-
None
-
Affects Version/s: None
-
Component/s: None
-
Environment:VS2013, C#, Win8.1, etc...
*Location*: http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/
*User-Agent*: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
*Referrer*: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/
*Screen Resolution*: 1680 x 1050
*repo*: docs-ecosystem
*source*: tutorial/serialize-documents-with-the-csharp-driver
VS2013, C#, Win8.1, etc... *Location*: http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/ *User-Agent*: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36 *Referrer*: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-csharp-driver/ *Screen Resolution*: 1680 x 1050 *repo*: docs-ecosystem *source*: tutorial/serialize-documents-with-the-csharp-driver
Hi
I was trying to get the "Rename" code working and I think I stumbled across a possible issue with the example.
Steps:
1. Create Person class with Name and Age properties
2. Serialized an instance of Person to my MongoDb
3. Code returns collection of Person and displays to console.
4. Changed Person class to Name and OldAge properties
5. Serialized an instance of Person to my MongoDb
6. When attempting to display collection to console, it excepts.
Code:
public class Person : ISupportInitialize
{
public ObjectId Id
public string Name { get; set; }
public int OldAge
{ get; set; }[BsonExtraElements]
public IDictionary<string, object> ExtraElements { get; set; }
public void BeginInit()
{ // } public void EndInit()
{
object ageValue;
if (!ExtraElements.TryGetValue("Alive", out ageValue))
{ return; }var age = (int)ageValue;
ExtraElements.Remove("Age");
OldAge = age;
}
}
Problem: When deserializing the second record, ExtraElements is null.
My resolution: The only way that I could make that code work was to do either of the following:
1. Add a constructor to new up ExtraElements
public Person()
{
ExtraElements = new Dictionary<string, object>();
}
2. Check if ExtraElements is null
if ((ExtraElements == null) || (!ExtraElements.TryGetValue("Age", out ageValue)))
Reporter: Richard O'Neil
E-mail: richard.oneil@garvin-allen.com