未知鉴别器值C#Mongo

未知鉴别器值C#Mongo,c#,mongodb,C#,Mongodb,我可以将我的类保存到mongo,但反序列化时出错 我得到一个错误: 'MongoDB.Bson.BsonSerializationException:未知的鉴别器值'ProductPropertyDefinition' 我需要帮助。如何告诉mongo正确地反序列化这个 public class Product { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string Id { get; set; }

我可以将我的类保存到mongo,但反序列化时出错

我得到一个错误:

'MongoDB.Bson.BsonSerializationException:未知的鉴别器值'ProductPropertyDefinition'

我需要帮助。如何告诉mongo正确地反序列化这个

public class Product
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    public ProductPropertyDefinitionCollection ProductProperties { get; set; }
}

public class ProductPropertyDefinitionCollection : CollectionBase
{
    public ProductPropertyDefinition this[int index]
    {
        get
        {
            return (ProductPropertyDefinition)List[index];
        }
        set
        {
            List[index] = value;
        }
    }

    public ProductPropertyDefinition this[string name]
    {
        get
        {
            return GetByName(name);
        }
    }

    public int Add(ProductPropertyDefinition value)
    {
        return List.Add(value);
    }

    public void Remove(ProductPropertyDefinition value)
    {
        List.Remove(value);
    }

    public bool Contains(ProductPropertyDefinition value)
    {
        return List.Contains(value);
    }

    private ProductPropertyDefinition GetByName(string propertyName)
    {
        ProductPropertyDefinition profileItem = null;

        foreach (ProductPropertyDefinition profileProperty in InnerList)
        {
            if (profileProperty.PropertyName == propertyName)
            {
                profileItem = profileProperty;
            }
        }
        return profileItem;
    }
}

明白了。我只需要添加一个类映射:

BsonClassMap.RegisterClassMap<ProductPropertyDefinition>();
BsonClassMap.RegisterClassMap();