C# MongoDB Upsert - 'BsonValue(Guid) 已过时:'Use the BsonBinaryData constructor instead and specify a Guid representation'

C# MongoDB Upsert - 'BsonValue(Guid) is obsolete: 'Use the BsonBinaryData constructor instead and specify a Guid representation'

我是 MongoDB 的新手,正在尝试创建更新插入:

public void UpsertRecord<T>(string collectionName, Guid id, T record)
    {
        var collection = db.GetCollection<T>(collectionName);

        var result = collection.ReplaceOne(
            new BsonDocument("_id", id),
            record,
            new ReplaceOptions { IsUpsert = true });
    }

但我在 Visual Studio 中收到以下警告:

'BsonValue.implicit operator BsonValue(Guid) is obsolete: Use the BsonBinaryData constructor instead and specify a Guid representation'

如有任何帮助,我们将不胜感激!

我是这样修复的:

public void UpsertRecord<T>(string table, Guid id, T record)
    {
        BsonBinaryData binData = new BsonBinaryData(id, GuidRepresentation.Standard);
        var collection = db.GetCollection<T>(table);

        var result = collection.ReplaceOne(
            new BsonDocument("_id", binData),
            record,
            new ReplaceOptions { IsUpsert = true });
    }