I write an extension method to implement the upsert(update if exists else insert) scenario but every time I use it, my records save with "0" object id in my mongodb document. here is my code:
public static async Task<ReplaceOneResult> UpsertAsync<T>
(this IMongoCollection<T> collection, T entity) where T : IEntity
{
return await collection.ReplaceOneAsync(i => i.Id == entity.Id,
entity,
new ReplaceOptions { IsUpsert = true });
}
and my model:
public class User : IEntity
{
[BsonId]
public ObjectId Id { get; set; }
public int TelegramUserId { get; set; }
public bool IsBot { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
public string ActivityPath { get; set; }
public string Number { get; set; }
public string Location { get; set; }
public ObjectId [] Playlists { get; set; }
}
and my saved record looks like this:
{
"_id": {
"$oid": "000000000000000000000000"
},
"TelegramUserId": 515151,
"IsBot": false,
"FirstName": "Test user",
"LastName": null,
"Username": "Test",
"ActivityPath": null,
"Number": null,
"Location": null,
"Playlists": null}
the problem is "000000000000000000000000" value for id, why id don't init correctly, I expect a valid guid value for that, not just zeros.
thanks for your help.