I hope someone can explain what is happening.
I created a repository class "InvoicesRepository.cs" that manage all the logic of listing, inserting, updating, deleting, etc. "Invoice.cs" objects. Is cleaner and easier to maintain.
public class InvoicesRepository
{
protected MySQLContext db;
public InvoicesRepository(MySQLContext db)
{
this.db = db;
}
public async void Insert(Invoice obj)
{
this.db.Invoices.Add(obj);
await this.db.SaveChangesAsync();
// performing other tasks...
}
// other useful methods...
}
There is a "InvoicesController.cs" with all the actions that i require. Inside this controller i create a "InvoiceTepository" obj and then use it to save information to the database. And, in every action
public class InvoicesController : Controller
{
private InvoicesRepository invoices;
public InvoicesController()
{
this.invoices = new InvoicesRepository(new MySQLContext());
}
[HttpPost]
public async Task<ActionResult> Upload(Invoice invoice)
{
try
{
this.invoices.Insert(invoice);
}
catch (DbEntityValidationException ex)
{
foreach (var eve in ex.EntityValidationErrors)
{
foreach (var err in eve.ValidationErrors)
{
ModelState.AddModelError(err.PropertyName, err.ErrorMessage);
}
}
}
catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
{
ModelState.AddModelError("", ex.ToString());
}
catch (System.Data.Entity.Core.UpdateException ex)
{
ModelState.AddModelError("", ex.ToString());
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
ModelState.AddModelError("", ex.ToString());
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.ToString());
}
return View();
}
// other useful action methods...
}
For testing, i'm inserting an "Invoice" object that has a duplicate data (unique column) in the database expecting to throw an exception and then my action catching it and display properly the error in the view but... the exception is "thrown" but is not "catched".
I debugged to see what kind of exceptions are thrown (including their inner exceptions) and added the required "catches" but still the exception is not "catched".
If i change the code of the controller to use the "MySQLContext.cs" class directly to save the info the exception is "catched":
[HttpPost]
public async Task<ActionResult> Upload(Invoice invoice)
{
try
{
// this is "catched" ¿?
this.db.Invoices.Add(obj);
await this.db.SaveChangesAsync();
}
catch (Exception ex)
{
ModelState.AddModelError("", ex.ToString());
}
return View();
}
Why is this happening? I need to be able catch any exception that my "Insert" or any other function in the "InvoiceRepository" class throw inside the controller.
Any help would be appreciated.