I am using EF6.1 and i would like to change the message to a more system specific message when the below exception is thrown.
Store update, insert, or delete statement affected an unexpected number of rows (0)
Now, my problem is i cannot seem to catch the exception? I have tried the following, but my breakpoints never seem to catch
public override System.Threading.Tasks.Task<int> SaveChangesAsync()
{
try
{
return base.SaveChangesAsync();
}
catch (System.Data.Entity.Core.OptimisticConcurrencyException e)
{
var a = e.Message;
}
catch (System.Data.DBConcurrencyException e)
{
var a = e.Message;
}
catch (Exception e)
{
var a = e.Message;
}
return null;
}
Is it not an exception that is thrown?? This is strange
Best How To :
You are executing an asynchronous method. This means that any exceptions will be thrown when you call await
on the returned task or when you try to retrieve the results using await myTask;
You never do so, which means that the exception is thrown and caught higher up your call stack.
If you want to catch the exception in your own method, you should change its syntax to
public override async System.Threading.Tasks.Task<int> SaveChangesAsync()
{
try
{
return await base.SaveChangesAsync();
}
I would be very cautious when overriding SaveChangesAsync
though. The OptimisticConcurrencyException exception contains the data that caused the optimistic conflict in its StateEntries
property. Replacing it with a nice message will only make it harder to decide how to handle a specific error and eg, decide to overwrite or reload.
Displaying a nice message is something best left to your controller.