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

Not ignoring readonly "id fields NamedIdMemberConvention leads to unique key violation

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 1.8.2
    • None
    • None

    Description

      The NUnit test below will throw a E1100 duplicate key error

       
      using MongoDB.Bson;
      using MongoDB.Driver;
      using NUnit.Framework;
       
      namespace MongoDB.BsonUnitTests.Jira
      {
          [TestFixture]
          public class ReadOnlyIdTests
          {
              public class ReadOnlyIdFieldClass
              {
                  public readonly int Id = 1;
                  public int id = 1;
              }
       
       
              [Test]
              public void TestSerialization()
              {
                  var obj1 = new ReadOnlyIdFieldClass();
                  obj1.id = 1;
                  var obj2 = new ReadOnlyIdFieldClass();
                  obj2.id = 2;
                  var client = new MongoClient();
                  var coll = client.GetServer().GetDatabase("foo").GetCollection<ReadOnlyIdFieldClass>("bar");
                  coll.Insert(obj1);
                  coll.Insert(obj2);
              }
          }
      }

      The fix is to either skip readonly fields as part of NameIdMemberConvention or implement it as a MemberMapConvention

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            sridhar Sridhar Nanjundeswaran
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: