[CSHARP-1076] Comment on: "mongodb-ecosystem/tutorial/serialize-documents-with-the-csharp-driver.txt" Created: 10/Jun/14  Updated: 04/Apr/15  Resolved: 04/Apr/15

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Blocker - P1
Reporter: Docs Collector User (Inactive) Assignee: Unassigned
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
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



 Description   

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

{ get; set; }
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


Generated at Wed Feb 07 21:38:36 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.