How can I confirm if an async EF6 await db.SaveChangesAsync() worked as expected?
From msdn:
public virtual Task<int> SaveChangesAsync()
Return Value Type: System.Threading.Tasks.Task A task that represents the asynchronous save operation. The task result contains the number of objects written to the underlying database.
Check if the result is greater than 0:
if(await db.SaveChangesAsync() > 0)
{
.....
}
More info here
Another option is to wrap this with try ... catch
block:
try
{
await db.SaveChangesAsync();
return Ok();
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
You can use the following below :)
try {
int writtenEntriesCount = await db.SaveChangesAsync();
if(writtenEntriesCount > 0){
// is saved
}else{
// is not saved
}
} catch(e) {
// handle error here
}