'Cannot implicitly convert type 'MongoDB.Driver.DeleteResult' to 'T'
I'm using MongoDB in my MVC project and am creating a utility function that I could use to delete records in database.
I am using :
- mongo db extension for C#
- C#(.net 4.6.1)
To method is passed record what I want to delete. I filter all recordings in DB by id of passed recording what I want to delete. Finding it I want to delete it. my method
public virtual async Task<T> Delete(EmailBlastDocument input)
{
var filter = Builders<T>.Filter.Eq(p => p.Id, input.Id);
var collection = DocumentContext.DocumentClient.GetDatabase(DatabaseName).GetCollection<T>(CollectionName);
return await collection.DeleteOneAsync(filter);
}
But it highlights error on compile time
Cannot implicitly convert type 'MongoDB.Driver.DeleteResult' to 'T'
How do I fix this function to properly return the result of delete record to the caller? And how to properly delete record in mongoDB
I have create Update method what was took as example
public virtual async Task<T> Update(T input)
{
var collection = DocumentContext.DocumentClient.GetDatabase(DatabaseName).GetCollection<T>(CollectionName);
try
{
await collection.ReplaceOneAsync(a => a.Id == input.Id, input);
}
catch (MongoWriteException ex)
{
switch (ex.WriteError.Category)
{
case ServerErrorCategory.ExecutionTimeout:
throw new TimeoutException();
default:
throw new CirrusDocumentGeneralException();
}
}
return input;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
