在反序列化到POCO对象期间,IgnoreExtraElementsConvention不会忽略_id

时间:2018-01-31 04:55:34

标签: c# mongodb deserialization

我正在使用官方MongoDB C#驱动程序的版本2.5,我遇到一个问题,当文档被反序列化为POCO对象时,IgnoreExtraElementsConvention对我没用。我得到的例外

[剪断]

System.FormatException
  HResult=0x80131537
  Message=Element '_id' does not match any field or property of class MongoDeserialization.Person.
  Source=MongoDB.Bson
  StackTrace:
   at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
   at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
   at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)

[/剪断]

但是,如果我在Person类上使用注释的BsonIgnoreExtraElements装饰器(甚至注释掉约定注册表块),我会得到所需的结果。所以,我想知道我在ConventionRegistryIgnoreExtraElementsConvention中缺少什么,以便让反序列化过程忽略我的_id类中未映射的“Person”字段。提前致谢。

请在下面找到准备运行的测试代码以重现问题:

using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;

namespace MongoDeserialization
{
    //[MongoDB.Bson.Serialization.Attributes.BsonIgnoreExtraElements]
    class Person
    {
        public string FirstName;
        public string LastName;
    }

    class MongoDBPersonReader
    {
        private IMongoCollection<Person> _collection;

        public MongoDBPersonReader(string CollectionName)
        {
            ConventionRegistry.Register("Ignore _id",
                                        new ConventionPack
                                        {
                                            new IgnoreExtraElementsConvention(true),
                                        },
                                        type => true);

            _collection = (new MongoClient("mongodb://localhost:27017/")).GetDatabase("my_db")
                                                                         .GetCollection<Person>(CollectionName);
        }

        public List<Person> GetAllPersonsFromCollection()
        {
            return _collection.Find(FilterDefinition<Person>.Empty).ToList();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            // Add some test data:
            List<InsertOneModel<Person>> list_of_insert_person_operations = new List<InsertOneModel<Person>>();
            (new List<Person>
            {
                new Person { FirstName = "Joe", LastName = "Smith" },
                new Person { FirstName = "Tom", LastName = "Smith" },
            }).ForEach(p => list_of_insert_person_operations.Add(new InsertOneModel<Person>(p)));
            (new MongoClient("mongodb://localhost:27017/")).GetDatabase("my_db")
                                                           .GetCollection<Person>("my_collection")
                                                           .BulkWrite(list_of_insert_person_operations);

            // Actual test for ignoring '_id' during deserialization:
            List<Person> persons = (new MongoDBPersonReader("my_collection")).GetAllPersonsFromCollection();
        }
    }
}

0 个答案:

没有答案