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

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.9.1
    • Affects Version/s: 1.9
    • Component/s: Serialization
    • None
    • Environment:
      Windows 8.1

      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.

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      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);
              }
          }
      }
      

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

              Created:
              Updated:
              Resolved: