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

Insert is not properly assigning the _id values when the nominal type is an interface

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 1.9.1
    • 1.9
    • Serialization
    • None
    • Windows 8.1

    Description

      When using Interfaces for class-definitions for documents and then using collection.Insert with this interface, ObjectId elements are always ObjectId.Empty. This does work correctly with version 1.8.3 and now broke with 1.9.0.

      Here is a very simple sample with the Person class which implements the IPerson interface. When using the IPerson interface in collection.Insert, the Id is always ObjectId.Empty, when casting the newPerson variable back to Person before inserting, it works correctly. When using c#-driver 1.8.3 (maybe also lower ones), it works with IPerson or casting to Person.

      using MongoDB.Bson;
      using MongoDB.Driver;
       
      namespace Mongo19Test
      {
          public interface IPerson { }
       
          public class Person : IPerson
          {
              public ObjectId Id { get; set; }
              public string Name { get; set; }
          }
       
          class Program
          {
              static void Main(string[] args)
              {
                  var client = new MongoClient("mongodb://localhost:27017");
                  var server = client.GetServer();
                  var database = server.GetDatabase("foo");
                  var collection = database.GetCollection<Person>("bar");
                  IPerson newPerson = new Person { Name = "Jack" };
                  collection.Insert(newPerson);
              }
          }
      }

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            Roemer Roman Bäriswyl [X]
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: