gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:07:08 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com