asp.net C# MongoDB MongoDB.Bson.BsonSerializationException:“”无法使用元素名称“”,因为它已被类型“”的属性“”使用,“

vltsax25  于 5个月前  发布在  .NET
关注(0)|答案(1)|浏览(55)

我有以下数据模型:(在C#中使用MongoDB)

public class aClass
{
    public virtual string Content { get; set; }
}
public class aClassDao
{
    public override string Content { get; set; }
}
public class MongoDbContext
{
    public IMongoDatabase Database { get; }
    public IMongoCollection<aClassDao> Classes=> Database.GetCollection<aClassDao>("Classes");

    public MongoDbContext()
    {
        // some connection logic
    }
}

当我启动应用程序时,我得到以下错误:

MongoDB.Bson.BsonSerializationException: The property 'Content' of type 'aClassDao' cannot use element name 
'Content' because it is already being used by property 'Content' of type 'aClass'.

kt06eoxx

kt06eoxx1#

我不认为有一个很好的方法基于这篇文章https://www.mongodb.com/community/forums/t/bsonserializationexception-when-inheriting-from-domain-object-class/159341
最后,我用自动Map器解决方案,它更直接。
基本上只需要创建一个新的对象,匹配的集合的类型

相关问题