Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1076

Comment on: "mongodb-ecosystem/tutorial/serialize-documents-with-the-csharp-driver.txt"

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Blocker - P1 Blocker - P1
    • None
    • Affects Version/s: None
    • Component/s: None
    • Environment:

      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

            Assignee:
            Unassigned Unassigned
            Reporter:
            xgen-internal-docs Docs Collector User (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: