Mongo C#-在MongoDB中映射IReadOnlyCollection

时间:2019-01-11 17:17:13

标签: c# mongodb mapping

我需要在MongoDB中映射一个IReadOnlyCollection,但是当我在Mongo中使用过滤器时会遇到问题。

我尝试使用“列表”字段并将“属性IReadOnlyCollection”映射到该字段,然后在“蒙哥”中映射该字段,但是有问题

public class Company : AggregateRoot
    {
        private List<CompanyDocument> _documents = new List<CompanyDocument>();

        public IReadOnlyCollection<CompanyDocument> Documents => _documents.AsReadOnly();

        public void AddDocument(Document document)
        {
            // ...

            _documents.Add(new CompanyDocument(document));
        }
BsonClassMap.RegisterClassMap<Company>(map =>
            {
                map.AutoMap();

                map.MapField("_adresses").SetElementName("Adresses");
                map.MapField("_documents").SetElementName("Documents");
                map.MapField("_emails").SetElementName("Emails");
                map.MapField("_phones").SetElementName("Phones");
            });
var filter = Builders<Company>.Filter.ElemMatch(
                c=>c.Documents,
                Builders<CompanyDocument>.Filter.Eq(companyDocument => 
                    companyDocument.Document.Value, "06923123000180"));

            var results = _companies
                .Find(filter)
                .Project(c => new CompanyItemModel
                {
//...
});


我认为输出是在文档列表中具有匹配元素的寄存器,但是会出现错误:

InvalidOperationException: Unable to determine the serialization information for c => c.Documents.
MongoDB.Driver.ExpressionFieldDefinition<TDocument>.Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)

0 个答案:

没有答案