BsonClassMap Serializer<xxx>' 无法使用“BsonRepresentation Attribute”类型的属性进行配置

Rag*_*h S 0 c# mongodb nosql

我是 MongoDB 的初学者。我在 Mongo 中有一个分层模型。请参阅下面我的代码。

public class Technology
{
    public Technology()
    {
        ProductGroups = new List<ProductGroup>().ToArray();
    }

    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }

    public string Name { get; set; }

    [BsonRepresentation(BsonType.Array)]
    public ProductGroup[] ProductGroups { get; set; }
}

public class ProductGroup
{
    public ProductGroup()
    {
        ProductTypes = new List<ProductType>().ToArray();
    }

    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }

    public string Name { get; set; }

    [BsonRepresentation(BsonType.Array)]
    public ProductType[] ProductTypes { get; set; }
}

public class ProductType
{

    [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }

    public string Name { get; set; }

}
Run Code Online (Sandbox Code Playgroud)

我成功地将Technology数据完全插入到文档中。

    public bool CreateTechnology(Technology model)
    {

        bool status = false;

        try
        {
            if (!string.IsNullOrEmpty(model.Name))
            {

                Collection = DataBase.GetCollection<Product>(TECHNOLOGY);

                BsonDocument product = new BsonDocument
                    {
                        {"Name",  model.Name.Trim()},
                        {"ProductGroups", new  BsonArray( new BsonValue []{})}
                    };

                Collection.Insert(product);

                status = true;

            }
        }
        catch
        {
            status = false;
        }

        return status;
    }
Run Code Online (Sandbox Code Playgroud)

请参阅 Robomongo 数据查看器。

在此输入图像描述

一切正常,但是当我尝试选择要列出的数据时,它显示了一个错误,例如

在此输入图像描述

模型中是否需要任何额外配置?

rno*_*nko 6

错误原因是BsonRepresentation数组属性上的属性。你应该删除它。此外,您可以BsonRepresentation从所有属性中删除。