c# - 删除实体类属性时 MongoDb.Driver 抛出异常

标签 c# mongodb migration

我是 MongoDb 的新手,使用一个简单的类并将具有这种结构的两条记录插入到数据库中。

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Property1 { get; set; }

    public override string ToString()
    {
        return $"{{ Id: {Id}, Name: {Name}  }}";
    }
}

我可以通过这段代码阅读它们,一切都很好。

var client = new MongoClient();
var db = client.GetDatabase("test-update");
var people = db.GetCollection<Person>("people").Find(p => true).ToList();

foreach (var person in people)
{
    Console.WriteLine(person.ToString());
}

结果是:

{ Id: 1, Name: person 1  }
{ Id: 2, Name: person 2  }

现在,如果我从我的 Person 类中删除 Property1,并再次运行读取的代码,我将遇到此错误:

Unhandled Exception: System.FormatException: Element 'Property1' does not match any field or property of class Person.
   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)
   at MongoDB.Driver.Core.Operations.CursorBatchDeserializationHelper.DeserializeBatch[TDocument](RawBsonArray batch, IBsonSerializer`1 documentSerializer, MessageEncoderSettings messageEncoderSet
tings)
   at MongoDB.Driver.Core.Operations.FindCommandOperation`1.CreateCursorBatch(BsonDocument result)
   at MongoDB.Driver.Core.Operations.FindCommandOperation`1.ExecuteCommand(IReadBinding binding, ServerDescription serverDescription, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindCommandOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
   at ConsoleApplication.Program.Main(String[] args) in C:\Users\choro\Desktop\mongo-update\Program.cs:line 12

这只是一个测试项目,元数据中的此类更改在实际项目中会一直发生。我如何管理这些更改以避免错误。

在 EF 和 SQL Server 中,我总是使用自动迁移,而不必考虑元数据更改。但是我不知道在 MongoDb 的情况下该怎么做。

谢谢

最佳答案

浏览 MongoDb 文档后,发现了一个对我有用的属性“[BsonIgnoreExtraElements]”。

[BsonIgnoreExtraElements]
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    //public string Property1 { get; set; }

    public override string ToString()
    {
        return $"{{ Id: {Id}, Name: {Name}  }}";
    }
}

官方的解释是这样的:

When a BSON document is deserialized, the name of each element is used to look up a matching member in the class map. Normally, if no matching member is found, an exception will be thrown. If you want to ignore extra elements during deserialization, use a BsonIgnoreExtraElementsAttribute

关于c# - 删除实体类属性时 MongoDb.Driver 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40818121/

相关文章:

c# - 通用打字困惑

javascript - Node.js错误: Can't set headers after they are sent.

sql - 在生产 mysql 数据库上进行复杂模式更新的最简单方法是什么?

django - PostgreSQL + Django + South 的第一步

mysql - 是否可以在 clojure 的 lobos 迁移插件中插入记录?

c# - SSMS SMO 对象 : Get query results

c# - 如何模拟 ObservableCollection

c# - 对象列表到预定类型列表

mongodb - hasMany关系为NULL时grails对象查询

mongodb - 如何使用 Mongo shell 命令在数组中插入对象?