I want to call
SaveChangesAsyncand when this succeeds, clear a list.
You don't need ContinueWith, you can just write it like this
public async Task<int> CommitAsync()
{
var result = await DataContext.SaveChangesAsync();
_addedEntities.Clear();
_changedEntities.Clear();
_deletedEntities.Clear();
return result;
}
In the original code, the catch block will never run.
Consider what happens when SaveChangesAsync throws a DbEntityValidationException: antecedent.Result will throw an AggregateException wrapping the DbEntityValidationException.
It's poor practice to throw an Exception as it means client code cannot be specific about the exceptions it catches. I would suggest changing RethrowException to a method that constructs the exception message you want, then calling it like this:
public async Task<int> CommitAsync()
{
try
{
var result = await DataContext.SaveChangesAsync();
_addedEntities.Clear();
_changedEntities.Clear();
_deletedEntities.Clear();
return result;
}
catch (DbEntityValidationException e)
{
var exceptionMessage = GetDbEntityValidationExceptionMessage(e);
throw new DbEntityValidationException(exceptionMessage, e);
}
}
This also removes the return default(int); which is just there to appease the compiler.