简体   繁体   中英

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

I'm new to MongoDB and am trying to create an Upsert:

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 });
    }

But I get the following warning in Visual Studio:

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

Any help would be much appreciated!

I was able to fix it this way:

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 });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM