我是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的情况下该怎么做。
由于
答案 0 :(得分:3)
在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} }}";
}
}
官方解释如下:
反序列化BSON文档时,使用每个元素的名称 在类映射中查找匹配的成员。通常,如果没有 找到匹配成员,将抛出异常。如果你想 在反序列化期间忽略额外的元素,使用a BsonIgnoreExtraElementsAttribute
答案 1 :(得分:0)
通过从模型中删除属性,您的模型和数据库文档不会同步,因为mongo无法将文档反序列化到您的模型。你也应该从mongo文件中删除Property1。